Untitled

 avatar
unknown
c_cpp
3 years ago
2.2 kB
6
Indexable
#pragma once

#include "../screen.h"
#include "../low_level.h"
#include <stdint.h>

#define SECTOR_SIZE 256

#define ATA_PORT 0x1F0


//registers
#define SECTOR_COUNT_REGISTER 2
#define SECTOR_NUMBER_REGISTER 3
#define SECTOR_NUMBER_LOW_REGISTER 4 
#define SECTOR_NUMBER_HIGH_REGISTER 5

#define STATUS_REGISTER 7

//statuses
#define BSY_RDY 0x80
#define DRQ_RDY 0x8


void read(uint32_t target_address, uint32_t address, uint8_t sector_count);
void write(uint32_t address, unsigned char sector_count, uint32_t* data);
void waitBSY();
void waitDRQ();


DISK.cpp:

#include "disk.h"

void read(uint32_t target_address, uint32_t address, uint8_t sector_count)
{
    waitBSY();
    port_byte_out(ATA_PORT+SECTOR_COUNT_REGISTER, sector_count);
    port_byte_out(ATA_PORT+SECTOR_NUMBER_REGISTER, address);
    port_byte_out(ATA_PORT+SECTOR_NUMBER_LOW_REGISTER, address >> 8);
    port_byte_out(ATA_PORT+SECTOR_NUMBER_HIGH_REGISTER, address >> 16);
    port_byte_out(STATUS_REGISTER, 0x20); //READ COMMAND

    uint16_t *target = (uint16_t*) target_address;
    for(int i = 0; i < sector_count; i++)
    {
        waitBSY();
        waitDRQ();
        for(int j = 0; j < SECTOR_SIZE; j++)
        {
            //target[i] = port_byte_in(ATA_PORT);
            //my_print("z", 0);
            print_int(1+port_byte_in(ATA_PORT), 0);
        }
        target+=256;
    }
    my_print("S", 0);
}

void write(uint32_t address, uint8_t sector_count, uint32_t* data)
{
    waitBSY();
    port_byte_out(ATA_PORT+SECTOR_COUNT_REGISTER, sector_count);
    port_byte_out(ATA_PORT+SECTOR_NUMBER_REGISTER, address);
    port_byte_out(ATA_PORT+SECTOR_NUMBER_LOW_REGISTER, address >> 8);
    port_byte_out(ATA_PORT+SECTOR_NUMBER_HIGH_REGISTER, address >> 16);
    port_byte_out(STATUS_REGISTER, 0x30); // WRITE COMMAND
    for(int i = 0; i < sector_count; i++)
    {
        waitBSY();
        waitDRQ();
        for(int j = 0; j < SECTOR_SIZE; j++)
        {
            port_long_out(ATA_PORT, 1);
        }
    }
}


void waitBSY()
{
    while(port_byte_in(ATA_PORT+STATUS_REGISTER) & BSY_RDY);
}

void waitDRQ()
{
    while(!port_byte_in(ATA_PORT+STATUS_REGISTER) & DRQ_RDY);
}
Editor is loading...