#include "stdafx.h"
#include "UI.h"
#include "Objects.h"
#include "Dispatch.h"
#include "CSB.h"
#include "Transition.h"

#include <string.h>
//#include <TextUtils.h>




#define	A0B(x)	( (unsigned char*)A0)[x]
#define	A0W(x)	((unsigned short*)(((unsigned char*)A0)+x))[0]
#define	A0L(x)	((unsigned long*)(((unsigned char*)A0)+x))[0]

#define	A1B(x)	( (unsigned char*)A1)[x]
#define	A1W(x)	((unsigned short*)(((unsigned char*)A1)+x))[0]
#define	A1L(x)	((unsigned long*)(((unsigned char*)A1)+x))[0]


#define	A5B(x)	( (unsigned char*)A5)[x]
#define	A5W(x)	((unsigned short*)(((unsigned char*)A5)+x))[0]
#define	A5L(x)	((unsigned long*)(((unsigned char*)A5)+x))[0]

#define	A6B(x)	( (unsigned char*)A6)[x]
#define	A6W(x)	((unsigned short*)(((unsigned char*)A6)+x))[0]
#define	A6L(x)	((unsigned long*)(((unsigned char*)A6)+x))[0]


static unsigned char 	sectors3_3 [1536] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x53,0x55,0x50,0x45,
0x52,0x47,0x41,0x55,0x30,0x30,0x30,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x41,0x55,0x54,0x4F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF4,0x01,
0x00,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0xE5,0x55,0x4E,0x47,0x45,0x4F,0x4E,0x20,0x50,0x52,0x47,0x20,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x57,0x00,0x74,0x0B,0x09,0x00,0xAC,0x29,0x02,0x00,0x44,0x55,0x4E,0x47,0x45,0x4F,0x4E,0x20,0x43,0x46,0x50,0x20,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x39,0x01,0x47,0x90,0x40,0x00,0xC6,0x4A,0x00,0x00,0x44,0x55,0x4E,0x47,0x45,0x4F,
0x4E,0x20,0x44,0x41,0x54,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x01,0x47,0x90,0x53,0x00,0xA2,0x82,0x00,0x00,
0x44,0x55,0x4E,0x47,0x45,0x4F,0x4E,0x20,0x45,0x58,0x45,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x43,0x01,0x47,0x90,
0x74,0x00,0xAC,0x29,0x02,0x00,0x47,0x52,0x41,0x50,0x48,0x49,0x43,0x53,0x44,0x41,0x54,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x51,0x01,0x47,0x90,0xFF,0x00,0x2D,0x26,0x04,0x00,0x53,0x45,0x43,0x54,0x4F,0x52,0x53,0x20,0x20,0x20,0x20,0x20,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6F,0x01,0x47,0x90,0x09,0x02,0x00,0x10,0x00,0x00,0x53,0x57,0x4F,0x4F,0x53,0x48,0x20,0x20,
0x49,0x4D,0x47,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x71,0x01,0x47,0x90,0x0D,0x02,0xC8,0x0B,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00};

static	char	bootsector[512] = {0x60,0x38,0x4C,0x6F,0x61,0x64,0x65,0x72,0x00,0x00,0x00,0x00,0x02,0x02,0x01,0x00,0x02,0x10,0x00,0x24,0x03,0xF8,0x02,0x00,0x0A,0x00,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x53,0x57,0x4F,0x4F,0x53,0x48,
0x20,0x20,0x49,0x4D,0x47,0x00,0x70,0x07,0x32,0x3C,0x07,0x77,0x48,0xE7,0xC0,0x00,0x3F,0x3C,0x00,0x25,0x4E,0x4E,0x54,0x8F,0x4C,0xDF,
0x00,0x03,0x20,0x7C,0xFF,0xFF,0x82,0x40,0x30,0x81,0x04,0x41,0x01,0x11,0x51,0xC8,0xFF,0xE2,0x20,0x7C,0xFF,0xFF,0x82,0x40,0x70,0x0F,
0x42,0x58,0x51,0xC8,0xFF,0xFC,0x33,0xFA,0xFF,0xAE,0x00,0x00,0x04,0x82,0x3F,0x39,0x00,0x00,0x04,0x46,0x3F,0x3C,0x00,0x07,0x4E,0x4D,
0x58,0x4F,0x4A,0x80,0x67,0x00,0x00,0xF6,0x2A,0x40,0x41,0xFA,0xFF,0x9C,0x4A,0x90,0x66,0x06,0x20,0xB9,0x00,0x00,0x04,0x32,0x30,0x2D,
0x00,0x08,0xE1,0x48,0xD0,0x80,0x38,0x40,0xD9,0xFA,0xFF,0x84,0x30,0x3A,0xFF,0x76,0x67,0x10,0x3C,0x3A,0xFF,0x72,0x38,0x3A,0xFF,0x70,
0x26,0x7A,0xFF,0x6E,0x60,0x00,0x00,0xB4,0x3C,0x2D,0x00,0x0A,0x38,0x2D,0x00,0x08,0xD8,0x6D,0x00,0x06,0x26,0x7A,0xFF,0x5E,0x61,0x00,
0x00,0xB0,0x66,0x00,0x00,0xAA,0x20,0x4C,0x30,0x2D,0x00,0x06,0xE1,0x48,0xE3,0x48,0x41,0xF0,0x00,0x00,0x43,0xFA,0xFF,0x48,0x90,0xFC,
0x00,0x20,0xB1,0xCC,0x6D,0x00,0x00,0x8E,0x70,0x0A,0x12,0x30,0x00,0x00,0xB2,0x31,0x00,0x00,0x66,0xEA,0x51,0xC8,0xFF,0xF4,0x7E,0x00,
0x1E,0x28,0x00,0x1B,0xE1,0x4F,0x1E,0x28,0x00,0x1A,0x2C,0x7A,0xFF,0x1A,0x26,0x7A,0xFF,0x12,0x42,0x84,0x0C,0x47,0x0F,0xF0,0x6C,0x52,
0x36,0x07,0x55,0x43,0xC6,0xED,0x00,0x02,0xD6,0x6D,0x00,0x0C,0x0C,0x44,0x00,0x40,0x6C,0x08,0x4A,0x44,0x67,0x0E,0xB6,0x45,0x67,0x10,
0x61,0x46,0x66,0x42,0xE1,0x8C,0xE3,0x8C,0xD7,0xC4,0x3C,0x03,0x3A,0x03,0x42,0x84,0xD8,0x6D,0x00,0x02,0xDA,0x6D,0x00,0x02,0x34,0x07,
0xE2,0x4A,0xD4,0x47,0x12,0x36,0x20,0x01,0xE1,0x49,0x12,0x36,0x20,0x00,0x08,0x07,0x00,0x00,0x67,0x02,0xE8,0x49,0x02,0x41,0x0F,0xFF,
0x3E,0x01,0x60,0xA8,0x4A,0x44,0x67,0x04,0x61,0x0A,0x66,0x06,0x2F,0x3A,0xFE,0xAC,0x4E,0x75,0x60,0xFE,0x3F,0x39,0x00,0x00,0x04,0x46,
0x3F,0x06,0x3F,0x04,0x2F,0x0B,0x42,0x67,0x3F,0x3C,0x00,0x04,0x4E,0x4D,0xDE,0xFC,0x00,0x0E,0x4A,0x40,0x4E,0x75,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x24,0x7D};

struct bpb 
{
        WORD    recsiz;         // sector size in bytes //
        WORD    clsiz;          // cluster size in sectors //
        WORD    clsizb;         // cluster size in bytes //
        WORD    rdlen;          // root directory length in records //
        WORD    fsiz;             // fat size in records //
        WORD    fatrec;         // first fat record (of last fat) //
        WORD    datrec;         // first data record //
        WORD    numcl;          // number of data clusters available //
        WORD    b_flags;
};

static struct flop_info {
  WORD cur_track;
  WORD rate;
  WORD spt;          // number of sectors per track //
  WORD sides;        // number of sides, or -1 if geometry not inited //
  BYTE serial[3];    // the serial number taken from the bootsector //
  BYTE wp;           // != 0 means write protected //
} finfo[2];


static struct bpb flop_bpb[2];


struct bs {
  /*   0 */  UBYTE bra[2];
  /*   2 */  UBYTE loader[6];
  /*   8 */  UBYTE serial[3];
  /*   b */  UBYTE bps[2];    // bytes per sector */
  /*   d */  UBYTE spc;       // sectors per cluster */
  /*   e */  UBYTE res[2];    // number of reserved sectors */
  /*  10 */  UBYTE fat;       // number of FATs */
  /*  11 */  UBYTE dir[2];    // number of DIR root entries */
  /*  13 */  UBYTE sec[2];    // total number of sectors */
  /*  15 */  UBYTE media;     // media descriptor */
  /*  16 */  UBYTE spf[2];    // sectors per FAT */
  /*  18 */  UBYTE spt[2];    // sectors per track */
  /*  1a */  UBYTE sides[2];  // number of sides */
  /*  1c */  UBYTE hid[2];    // number of hidden sectors */
  /*  1e */  UBYTE data[0x1e0];
  /* 1fe */  UBYTE cksum[2];
};

typedef struct bpb BPB;
static char				memory[8192]={'m','e','m','o','r','y',0,};

static unsigned char  	_bsdata[] = {	/*0xA10*/0x00,0x00,
										/*0xA12*/0x00,0x00,0x00,0x00,
										/*0xA16*/0x00,0x04,0x00,0x00,
										/*0xA1A*/0x00,0x00,0x00,0x00,
										/*0xA1E*/'S','W','O','O','S','H',' ',' ','I','M','G',0x00};

static unsigned short*  A10=(unsigned short*)&_bsdata[0];
static unsigned short*  A12=(unsigned short*)&_bsdata[2];
static unsigned short*  A14=(unsigned short*)&_bsdata[4];
static unsigned long*  	A16=(unsigned long*)&_bsdata[6];
static unsigned long*  	A1A=(unsigned long*)&_bsdata[10];
static unsigned char*	A1E=&_bsdata[14];
static long				_432 = (long)memory;

i32 _0DD78B70(i16,i16,i32);
void A16LDCode();


/*

 0- 1	bra
 2- 7 	loader
 8- a 	serial number
 b- c 	bytes per sector
 d 	  	sectors per cluster
 e- f 	number of reserved sectors
10		number of fatek
11-12	number of root dir entries
13-14	total number of sectors
15		media descriptor
16-17	sectors per fat
18-19	sectors per track
1a-1b	number of sides
1c-1d	number of hidden sectors
1e-1fd	data
1fe-1ff	checksum

*/

static UWORD getiword(UBYTE *addr)
{
  UWORD value;
  value = (((UWORD)addr[1])<<8) + addr[0]; 
  return value;
}

static LONG flop_getbpb(WORD dev)
{
  struct bs *b;
  LONG tmp;
  WORD err;
  
  if(dev < 0 || dev > 1) return 0;
  
  /* read bootsector */
  b = (struct bs *)bootsector;
  
  flop_bpb[dev].recsiz = getiword(b->bps);
  flop_bpb[dev].clsiz = b->spc;
  flop_bpb[dev].clsizb = flop_bpb[dev].clsiz * flop_bpb[dev].recsiz;
  tmp = getiword(b->dir);
  flop_bpb[dev].rdlen = (tmp * 32) / flop_bpb[dev].recsiz;
  flop_bpb[dev].fsiz = getiword(b->spf);

  /* the structure of the floppy is assumed to be:
   * - bootsector
   * - fats
   * - dir
   * - data clusters
   * TODO: understand what to do with reserved or hidden sectors.
   */

  flop_bpb[dev].fatrec = 1 + flop_bpb[dev].fsiz; 
  flop_bpb[dev].datrec = flop_bpb[dev].fatrec + flop_bpb[dev].fsiz 
                         + flop_bpb[dev].rdlen;
  flop_bpb[dev].numcl = (getiword(b->sec) - flop_bpb[dev].datrec) / b->spc;
  flop_bpb[dev].b_flags = 0;   // assume floppies are always in FAT12 //
  
  // additional geometry info //
  finfo[dev].sides = getiword(b->sides);
  finfo[dev].spt = getiword(b->spt);
  finfo[dev].serial[0] = b->serial[0];
  finfo[dev].serial[1] = b->serial[1];
  finfo[dev].serial[2] = b->serial[2];
  
  return (LONG) &flop_bpb[dev];
}


/*			0DD78B70   MOVE.W     $00000446,-(A7)                         | 3F39 0000 0446
            0DD78B76   MOVE.W     D6,-(A7)                                | 3F06
            0DD78B78   MOVE.W     D4,-(A7)                                | 3F04
            0DD78B7A   MOVE.L     A3,-(A7)                                | 2F0B
            0DD78B7C   CLR.W      -(A7)                                   | 4267
            0DD78B7E   MOVE.W     #$0004,-(A7)                            | 3F3C 0004
*/

static short RWABS(short dev, short sector, short count, long addr, short flag, short sel)
{
	dev = dev;
	sector = sector;
	count = count;
	addr = addr;
	flag = flag;
	
	if(sector == 3 && count == 3)
	{
		memmove((void*)addr,sectors3_3,1536);
	}
	return 0;
}

static i8 	SWOOSHScreen[320*200/2]; 
static i16 FFFF8240[16] = {	0x777, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0x555, 
									0x333, 0x733, 0x373, 0x773, 0x337, 0x737, 0x377, 0x000};
									
									
void swoosh_bootsector();
extern void UpdateScreenArea(
                      i8  *STScreen,
                      int  x0,
                      int  y0,
                      int  width,
                      int  height,
                      i16 *palette,
                      bool paletteChanged,
                      i32 *pOldChecksum);

void VSYNC()
{
	i32 ignore;
	UpdateScreenArea(SWOOSHScreen,0,0,320,200,FFFF8240,TRUE,&ignore);
}

                      
void swoosh()
{
	i32	ignore;
	
	UpdateScreenArea(SWOOSHScreen,0,0,320,200,FFFF8240,TRUE,&ignore);

	swoosh_bootsector();
}

void swoosh_bootsector()
{
	dReg D0,D1,D2,D3,D4,D5,D6,D7;
	unsigned long A0,A1,A2,A3,A4,A5,A6,A7;

/*            0DD789F0   BRA.S      *+$003A                    ; 0DD78A2A   | 6038
*/			  goto _0DD78A2A;
/*            0DD789F2   TDIVU.L    $6572(A7),D4:D6                         | 4C6F 6164 6572
              0DD789F8   ORI.B      #$00,D0                                 | 0000 0000
              0DD789FC   ANDI.B     #$0100,D2                               | 0202 0100
              0DD78A00   ANDI.B     #$24,(A0)                  ; '$'        | 0210 0024
              0DD78A04   BSET       D1,SPAlarm                              | 03F8 0200
              0DD78A08   EORI.B     #$0100,D0                               | 0A00 0100
              0DD78A0C   ORI.B      #$00,D0                                 | 0000 0000
              0DD78A10   ORI.B      #$00,D0                                 | 0000 0000
              0DD78A14   ORI.B      #$04,D0                                 | 0000 0004
              0DD78A18   ORI.B      #$00,D0                                 | 0000 0000
              0DD78A1C   ORI.B      #$5357,D0                  ; 'W'        | 0000 5357
              0DD78A20   DC.W       $4F4F                      ; ????       | 4F4F
              0DD78A22   SUBQ.W     #$1,A0                                  | 5348
              0DD78A24   MOVE.L     -(A0),D0                                | 2020
              0DD78A26   DC.W       $494D                      ; ????       | 494D
              0DD78A28   CHK.L      D0,D3                                   | 4700
*/

_0DD78A2A:
//            0DD78A2A   MOVEQ      #$07,D0                                 | 7007
//            0DD78A2C   MOVE.W     #$0777,D1                               | 323C 0777
	D0L	=	7;
	D1W	=	0x777;
//            0DD78A30   MOVEM.L    D0/D1,-(A7)                             | 48E7 C000

//            0DD78A34   MOVE.W     #$0025,-(A7)                            | 3F3C 0025
//            0DD78A38   TRAP       #$E                                     | 4E4E
//            0DD78A3A   ADDQ.L     #$2,A7                                  | 548F
_0DD78A30:

	VSYNC();
//            0DD78A3C   MOVEM.L    (A7)+,D0/D1                             | 4CDF 0003
//            0DD78A40   MOVEA.L    #$FFFF8240,A0                           | 207C FFFF 8240
	A0 = (long)FFFF8240;
//            0DD78A46   MOVE.W     D1,(A0)                                 | 3081
//            0DD78A48   SUBI.W     #$0111,D1                               | 0441 0111
//            0DD78A4C   DBF        D0,*-$001C                 ; 0DD78A30   | 51C8 FFE2
 	A0W(0) = D1W;
 	D1W -= 0x111;
 	if(D0W-->=0) goto _0DD78A30;
 	

//            0DD78A50   MOVEA.L    #$FFFF8240,A0                           | 207C FFFF 8240
//            0DD78A56   MOVEQ      #$0F,D0                                 | 700F
//            0DD78A58   CLR.W      (A0)+                                   | 4258
//            0DD78A5A   DBF        D0,*-$0002                 ; 0DD78A58   | 51C8 FFFC

 	A0 = (long)FFFF8240;
	D0L = 0xF;

_0DD78A58:

  	A0W(0) = 0; A0+=2;
 	if(D0W-->=0) goto _0DD78A58;
 	
 	
//            0DD78A5E   MOVE.W     *-$0050,$00000482          ; 0DD78A0E   | 33FA FFAE 0000	
//            0DD78A66   MOVE.W     $00000446,-(A7)                         | 3F39 0000 0446
//            0DD78A6C   MOVE.W     #$0007,-(A7)                            | 3F3C 0007
//            0DD78A70   TRAP       #$D                                     | 4E4D
//            0DD78A72   ADDQ.W     #$4,A7                                  | 584F
//            0DD78A74   TST.L      D0                                      | 4A80
//            0DD78A76   BEQ        *+$00F8                    ; 0DD78B6E   | 6700 00F6
//
//  		  reading bios parameter block. we already have it in memory :-)
 	
 	D0L = flop_getbpb(0);
	if(!D0L) goto _0DD78B6E;
			 			
//            0DD78A7A   MOVEA.L    D0,A5                                   | 2A40
//            0DD78A7C   LEA        *-$0062,A0                 ; 0DD78A1A   | 41FA FF9C
//            0DD78A80   TST.L      (A0)                                    | 4A90
//            0DD78A82   BNE.S      *+$0008                    ; 0DD78A8A   | 6606
//            0DD78A84   MOVE.L     $00000432,(A0)                          | 20B9 0000 0432
 	A5 = D0L; 
	A0 = (long)A1A;
	if(A0L(0)!=0)	goto _0DD78A8A;
	A0L(0) = _432;

_0DD78A8A:
//            0DD78A8A   MOVE.W     $0008(A5),D0                            | 302D 0008
//            0DD78A8E   LSL.W      #$8,D0                                  | E148
//            0DD78A90   ADD.L      D0,D0                                   | D080
//            0DD78A92   MOVEA.W    D0,A4                                   | 3840
	D0L = A5W(8);
  	D0W = D0W << 8;
	D0L = D0L + D0L;
  	A4  = D0W;

//            0DD78A94   ADDA.L     *-$007A,A4                 ; 0DD78A1A   | D9FA FF84
//            0DD78A98   MOVE.W     *-$0088,D0                 ; 0DD78A10   | 303A FF76
//            0DD78A9C   BEQ.S      *+$0012                    ; 0DD78AAE   | 6710
	A4 += A1A[0];
	D0L = A10[0];
	if(!D0W) goto _0DD78AAE;

//            0DD78A9E   MOVE.W     *-$008C,D6                 ; 0DD78A12   | 3C3A FF72
//            0DD78AA2   MOVE.W     *-$008E,D4                 ; 0DD78A14   | 383A FF70
//            0DD78AA6   MOVEA.L    *-$0090,A3                 ; 0DD78A16   | 267A FF6E
//            0DD78AAA   BRA        *+$00B6                    ; 0DD78B60   | 6000 00B4
	D6L = A12[0];
	D4L = A14[0];
	A3 = (long)A16[0];
	goto _0DD78B60;

_0DD78AAE:
//            0DD78AAE   MOVE.W     $000A(A5),D6                            | 3C2D 000A
//            0DD78AB2   MOVE.W     $0008(A5),D4                            | 382D 0008
//            0DD78AB6   ADD.W      $0006(A5),D4                            | D86D 0006
//            0DD78ABA   MOVEA.L    *-$00A0,A3                 ; 0DD78A1A   | 267A FF5E
//            0DD78ABE   BSR        *+$00B2                    ; 0DD78B70   | 6100 00B0
//			  0DD78AC2   BNE        *+$00AC                    ; 0DD78B6E   | 6600 00AA
	D6L = A5W(0xA);
	D4L = A5W(0x8);
	D4W += A5W(0x6);
	A3 = (long)A1A[0];
 	D0L = _0DD78B70(D6W,D4W,A3);
	if(D0L) goto _0DD78B6E;

//            0DD78AC6   MOVEA.L    A4,A0                                   | 204C
//            0DD78AC8   MOVE.W     $0006(A5),D0                            | 302D 0006
//            0DD78ACC   LSL.W      #$8,D0                                  | E148
//            0DD78ACE   LSL.W      #$1,D0                                  | E348
//            0DD78AD0   LEA        $00(A0,D0.W),A0                         | 41F0 0000
//            0DD78AD4   LEA        *-$00B6,A1                 ; 0DD78A1E   | 43FA FF48
	A0 = A4;
	D0L = A5W(6);
	D0W <<= 9;
	A0 = A0 + D0W;
	A1 = (long)A1E;

_0DD78AD8:
//            0DD78AD8   SUBA.W     #$0020,A0                               | 90FC 0020
//            0DD78ADC   CMPA.L     A4,A0                                   | B1CC
//            0DD78ADE   BLT        *+$0090                    ; 0DD78B6E   | 6D00 008E
	A0 -= 0x20;
	if(A0 < A4)  goto _0DD78B6E;

//            0DD78AE2   MOVEQ      #$0A,D0                                 | 700A
	D0L = 0xA;
_0DD78AE4:
//            0DD78AE4   MOVE.B     $00(A0,D0.W),D1                         | 1230 0000
//            0DD78AE8   CMP.B      $00(A1,D0.W),D1                         | B231 0000
//	          0DD78AEC   BNE.S      *-$0014                    ; 0DD78AD8   | 66EA
//            0DD78AEE   DBF        D0,*-$000A                 ; 0DD78AE4   | 51C8 FFF4
	D1B = A0B(D0W);
	if(A1B(D0W) != D1B) goto _0DD78AD8;
	if(D0W-->=0) goto _0DD78AE4;
			
//            0DD78AF2   MOVEQ      #$00,D7                                 | 7E00
//            0DD78AF4   MOVE.B     $001B(A0),D7                            | 1E28 001B
//            0DD78AF8   LSL.W      #$8,D7                                  | E14F
//            0DD78AFA   MOVE.B     $001A(A0),D7                            | 1E28 001A
//            0DD78AFE   MOVEA.L    *-$00E4,A6                 ; 0DD78A1A   | 2C7A FF1A
//            0DD78B02   MOVEA.L    *-$00EC,A3                 ; 0DD78A16   | 267A FF12
//            0DD78B06   CLR.L      D4                                      | 4284
	D7L = 0;
	D7B = A0B(0x1B);
	D7W <<= 8;
	D7B = A0B(0x1A);
	A6 = (long)A1A[0];
	A3 = (long)A16[0];
	D4L = 0;
	
_0DD78B08:
//            0DD78B08   CMPI.W     #$0FF0,D7                               | 0C47 0FF0
//            0DD78B0C   BGE.S      *+$0054                    ; 0DD78B60   | 6C52
//            0DD78B0E   MOVE.W     D7,D3                                   | 3607
//            0DD78B10   SUBQ.W     #$2,D3                                  | 5543
//            0DD78B12   MULU.W     $0002(A5),D3                            | C6ED 0002
//            0DD78B16   ADD.W      $000C(A5),D3                            | D66D 000C
//            0DD78B1A   CMPI.W     #$0040,D4                               | 0C44 0040
//            0DD78B1E   BGE.S      *+$000A                    ; 0DD78B28   | 6C08
//            0DD78B20   TST.W      D4                                      | 4A44
//            0DD78B22   BEQ.S      *+$0010                    ; 0DD78B32   | 670E
//            0DD78B24   CMP.W      D5,D3                                   | B645
//            0DD78B26   BEQ.S      *+$0012                    ; 0DD78B38   | 6710

	if(D7W >= 0xFF0) goto _0DD78B60;
	D3L = D7W;
	D3W-=2;
	D3W*=A5W(2);
	D3W+=A5W(0xC);
	if(D4W >= 0x40) goto _0DD78B28;
	if(D4W == 0)    goto _0DD78B32;
	if(D5W == D3W)  goto _0DD78B38;


_0DD78B28:
//			  0DD78B28   BSR.S      *+$0048                    ; 0DD78B70   | 6146
//			  0DD78B2A   BNE.S      *+$0044                    ; 0DD78B6E   | 6642
//            0DD78B2C   LSL.L      #$8,D4                                  | E18C
//            0DD78B2E   LSL.L      #$1,D4                                  | E38C
//            0DD78B30   ADDA.L     D4,A3                                   | D7C4
    D0L = _0DD78B70(D6W,D4W,A3);
	if(D0W)		goto _0DD78B6E;
    D4L <<= 9;
	A3 += D4L;


_0DD78B32:
//            0DD78B32   MOVE.W     D3,D6                                   | 3C03
//            0DD78B34   MOVE.W     D3,D5                                   | 3A03
//            0DD78B36   CLR.L      D4                                      | 4284
	D6W = D3W;
	D5W = D3W;
	D4L = 0;


_0DD78B38:
//            0DD78B38   ADD.W      $0002(A5),D4                            | D86D 0002
//            0DD78B3C   ADD.W      $0002(A5),D5                            | DA6D 0002
//            0DD78B40   MOVE.W     D7,D2                                   | 3407
//            0DD78B42   LSR.W      #$1,D2                                  | E24A
//            0DD78B44   ADD.W      D7,D2                                   | D447
//            0DD78B46   MOVE.B     $01(A6,D2.W),D1                         | 1236 2001
//            0DD78B4A   LSL.W      #$8,D1                                  | E149
//            0DD78B4C   MOVE.B     $00(A6,D2.W),D1                         | 1236 2000
//            0DD78B50   BTST       #$00,D7                                 | 0807 0000
//            0DD78B54   BEQ.S      *+$0004                    ; 0DD78B58   | 6702
//            0DD78B56   LSR.W      #$4,D1                                  | E849
	D4W += A5W(2);
	D5W += A5W(2);
	D2W = D7W;
	D2W >>= 1;
	D2W += D7W;
	D1L = A6B(1+D2W);
	D1W <<= 8;
	D1L = A6B(D2W);
	if(!(D7W&1)) goto _0DD78B58;
	D1W >>= 4;


_0DD78B58:
//            0DD78B58   ANDI.W     #$0FFF,D1                               | 0241 0FFF
//            0DD78B5C   MOVE.W     D1,D7                                   | 3E01
//            0DD78B5E   BRA.S      *-$0056                    ; 0DD78B08   | 60A8
	D1W &= 0xFFF;
	D7W = D1W;
	goto _0DD78B08;

_0DD78B60:
//            0DD78B60   TST.W      D4                                      | 4A44
//            0DD78B62   BEQ.S      *+$0006                    ; 0DD78B68   | 6704
//            0DD78B64   BSR.S      *+$000C                    ; 0DD78B70   | 610A
//            0DD78B66   BNE.S      *+$0008                    ; 0DD78B6E   | 6606
	if(!D4W) goto _0DD78B68;
	D0L = _0DD78B70(D6W,D4W,A3);
	if(D0L) goto _0DD78B6E;

_0DD78B68:
//			  0DD78B68   MOVE.L     *-$0152,-(A7)              ; 0DD78A16   | 2F3A FEAC
//			  0DD78B6C   RTS                                                | 4E75
A16LDCode();

_0DD78B6E:	  //DebugStr("\pST Code hanged here!");   
            /*0DD78B6E   BRA.S      *+$0000                    ; 0DD78B6E   | 60FE
            
              code here converted to subroutine _0DD78B70
            
            0DD78B8C   ORI.B      #$00,D0                                 | 0000 0000
            0DD78B90   ORI.B      #$00,D0                                 | 0000 0000
            0DD78B94   ORI.B      #$00,D0                                 | 0000 0000
            0DD78B98   ORI.B      #$00,D0                                 | 0000 0000
            0DD78B9C   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BA0   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BA4   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BA8   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BAC   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BB0   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BB4   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BB8   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BBC   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BC0   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BC4   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BC8   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BCC   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BD0   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BD4   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BD8   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BDC   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BE0   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BE4   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BE8   ORI.B      #$00,D0                                 | 0000 0000
            0DD78BEC   ORI.B      #$247D,D0                  ; '}'        | 0000 247D
			*/
	{}
}

i32 _0DD78B70(i16 d6, i16 d4, i32 a3)
{            
//			0DD78B70   MOVE.W     $00000446,-(A7)                         | 3F39 0000 0446
//            0DD78B76   MOVE.W     D6,-(A7)                                | 3F06
//            0DD78B78   MOVE.W     D4,-(A7)                                | 3F04
//            0DD78B7A   MOVE.L     A3,-(A7)                                | 2F0B
//            0DD78B7C   CLR.W      -(A7)                                   | 4267
//            0DD78B7E   MOVE.W     #$0004,-(A7)                            | 3F3C 0004
//            0DD78B82   TRAP       #$D                                     | 4E4D
//            0DD78B84   ADDA.W     #$000E,A7                               | DEFC 000E
//            0DD78B88   TST.W      D0                                      | 4A40
//           0DD78B8A   RTS                                                | 4E75
	return RWABS(0,d6,d4,a3,0,4);
}

void A16LDCode()
{
	//Debugger();
}
