#include "console.h"
#include "portio.h"
#include "dma.h"
#include "floppy.h"

#define DRIVE_A 0x00
#define DRIVE_B 0x01
#define DRIVE_C 0x02
#define DRIVE_D 0x03

#define PRIMARY 0x3F0
#define SECONDARY 0x370

#define REG_STATUS_A 0x00
#define REG_STATUS_B 0x01

#define REG_DOR 0x02
#define DOR_ENABLE 0x04 // 1 = enabled, 0 = reset
#define DOR_DMA 0x08 // DMA and IRQ enabled
#define DOR_MOTOR_A 0x10
#define DOR_MOTOR_B 0x20
#define DOR_MOTOR_C 0x40
#define DOR_MOTOR_D 0x80
// drive select from drive constants

#define REG_STATUS 0x04
#define STATUS_MRQ 0x80 // data register ready
#define STATUS_DIO 0x40 // 1 = controller->CPU 0 = CPU->controller
#define STATUS_NDMA 0x20 // non-DMA mode
#define STATUS_BUSY 0x10 // 1 = active, 0 = not active
#define STATUS_ACT_A 0x01
#define STATUS_ACT_B 0x02
#define STATUS_ACT_C 0x04
#define STATUS_ACT_D 0x08

#define REG_DSR 0x04
#define REG_DATA 0x05
#define REG_DIR 0x07

#define REG_CCR 0x07
#define CCR_500KBPS 0x00
#define CCR_300KBPS 0x01
#define CCR_250KBPS 0x02
#define CCR_1MBPS 0x03

#define DMA_CHANNEL 0x02

void *dma_buffer = (void *)0x10000; // let's keep it here for now

char *floppy_type_strings[6] = {
	"Not present",
	"360kB 5.25\"",
	"1.2mB 5.25\"",
	"720kB 3.5\"",
	"1.44mB 3.5\"",
	"2.88mB 3.5\""
};

unsigned char floppy_type[2];


// Floppy drive interrupt
void interrupt_38() {
	
}

void floppy_init() {
	// Query CMOS for drive types
	
	unsigned char b;
	outportb(0x70, 0x10);
	b = inportb(0x71);

	int i;
	for (i = 0; i <= 1; i++) {
		floppy_type[i] = (b & 0xF0) >> 4;

		if (floppy_type[i] > 5) {
			panic(" FLOPPY: Insane floppy type!\n");
		}

		printf(" FLOPPY: Drive %d: %s\n", i, floppy_type_strings[floppy_type[i]]);
		b <<= 4;
	}
}
