testpico: more on timer reload
[megadrive.git] / megaed-stop / edos.h
1 /* 
2  * File:   edos.h
3  * Author: krik
4  *
5  * Created on 29 Ìàðò 2012 ã., 1:29
6  */
7
8 #ifndef _EDOS_H
9 #define _EDOS_H
10
11 //#include "segalib.h"
12
13 /*
14  * saddr: address in sectors
15  * slen: lenght in sectors
16  * wr_slen: how many sectors will be written
17  * sector size 512 bytes
18  * dma reading of usb and SD available only if data destination located in rom area (0-0x3ffffff). dma reading not available for genesis ram
19  * destination address for dma reading must be aligned to 512b
20  * one mbyte of sdram can be mapped to one of four banks in rom space. osSetMemMap(u16) can be used for mapping configuration.
21  * os code located in last mbyte of sdram and should be mapped in bank 0
22  * user code located in begin of sdram. default mapping 0x210f means that end of sdram (OS) mapped to area 0-0x0fffff, first mbyte of user code mapped to 0x100000-0x1fffff,
23  * memory configuration:
24  * genesis address: 0x000000-0x0fffff, physical sdram address: 0xf00000-0xffffff  OS
25  * genesis address: 0x100000-0x3fffff, physical sdram address: 0x000000-0x2fffff  user code (current application)
26  * 0xff0000-0xff00ff OS dma code
27  * first 256bytes of genesis ram must be reserved for OS
28  * */
29
30
31 #define MEM_ERR_SPI_RD_TIMEOUT 120
32
33 #define ERR_FILE_TOO_BIG 140
34 #define ERR_WRON_OS_SIZE 142
35 #define ERR_OS_VERIFY 143
36 #define ERR_OS_VERIFY2 144
37 #define ERR_BAD_DMA_ADDRESS 145
38 #define ERR_MUST_NOT_RET 146
39
40 //100 - 119 fat errors
41 #define FAT_ERR_NOT_EXIST 100
42 #define FAT_ERR_EXIST 101
43 #define FAT_ERR_NAME 102
44 #define FAT_ERR_OUT_OF_FILE 103
45 #define FAT_ERR_BAD_BASE_CLUSTER 104;
46 #define FAT_ERR_NO_FRE_SPACE 105
47 #define FAT_ERR_NOT_FILE 106
48 #define FAT_ERR_FILE_MODE 107
49 #define FAT_ERR_ROT_OVERFLOW 108
50 #define FAT_ERR_OUT_OF_TABLE 109
51 #define FAT_ERR_INIT 110
52 #define FAT_LFN_BUFF_OVERFLOW 111
53 #define FAT_DISK_NOT_READY 112
54 #define FAT_ERR_SIZE 113
55 #define FAT_ERR_RESIZE 114
56
57
58 #define DISK_ERR_INIT 50
59 #define DISK_ERR_RD1 62
60 #define DISK_ERR_RD2 63
61
62 #define DISK_ERR_WR1 64
63 #define DISK_ERR_WR2 65
64 #define DISK_ERR_WR3 66
65 #define DISK_ERR_WR4 67
66 #define DISK_ERR_WR5 68
67
68 typedef struct {
69     u32 entry_cluster;
70     u32 size;
71     u32 hdr_sector;
72     u16 hdr_idx;
73     u8 name[256];
74     u16 is_dir;
75 } FatFullRecord;
76
77 typedef struct {
78     u8(*diskWrite)(u32 saddr, u8 *buff, u16 slen);
79     u8(*diskRead)(u32 saddr, void *buff, u16 slen);//*dst must be alligned to 512bytes and be in range 0x100000-0x3fffff. *dst can be located in genesis ram also, but transfer will be slow in this case
80     u8(*usbReadByte)();//loop inside of function waiting until usbRdReady != 0
81     void (*usbWriteByte)(u8 dat);//loop inside of function waiting until usbWrReady != 0
82     u8(*usbReadDma)(u16 *dst, u16 slen);//*dst must be alligned to 512bytes and be in range 0x100000-0x3fffff
83     u8(*usbReadPio)(u16 *dst, u16 slen);
84     u8(*usbRdReady)(); //return 1 if some data comes from pc
85     u8(*usbWrReady)(); //return 1 usb ready to send byte
86     void (*osSetMemMap)(u16 map); //memoty configuration 4x1Mb
87     u16(*osGetOsVersion)();
88     u16(*osGetFirmVersion)();
89     void (*osGetSerial)(u32 * buff); // 8bytes uniq serial number
90     void (*VDP_setReg)(u8 reg, u8 value);//os vdp functions should be used, otherwise system may hang while udb/sd dma
91     void (*VDP_vintOn)();
92     void (*VDP_vintOff)();
93     void (*memInitDmaCode)();//copy dma routine to genesis ram
94     u8(*fatReadDir)(FatFullRecord * frec);//get next record in current dir
95     void(*fatOpenDir)(u32 entry_cluster);//arg is entry cluster ot dir or 0. zero arg means that need to open root dir
96     u8(*fatOpenFileByeName)(u8 *name, u32 wr_slen);//wr_slen write len, or 0 if reading
97     u8(*fatOpenFile)(FatFullRecord *rec, u32 wr_slen);//wr_slen is write len, or 0 if reading
98     u8(*fatReadFile)(void *dst, u32 slen);
99     u8(*fatWriteFile)(void *src, u32 slen);
100     u8(*fatCreateRecIfNotExist)(u8 *name, u8 is_dir);
101     u8(*fatSkipSectors)(u32 slen);
102 } OsRoutine;
103
104
105 extern OsRoutine *ed_os;
106 void edInit();
107
108
109 //stereo dac. 8bit for each channel.
110 //any writing to this port enables dac output, any reading from this port disables dac output.
111 #define REG_DAC *((volatile u16*) (0xA1300E))
112
113
114 //stuff for direct access to usb and spi. not recommended for use. OS function should be used for best compatibility
115 #define _SPI_SS 0
116 #define _SPI_FULL_SPEED 1
117 #define _SPI_SPI16 2
118 #define _SPI_AREAD 3
119
120 //spi port (sd interface)
121 #define SPI_PORT *((volatile u16*) (0xA13000))
122 #define SPI_CFG_PORT *((volatile u16*) (0xA13002))
123
124 //16bit or 8bit spi mode
125 #define SPI16_ON SPI_CFG_PORT |= (1 << _SPI_SPI16)
126 #define SPI16_OFF SPI_CFG_PORT &= ~(1 << _SPI_SPI16)
127
128 //spi autoread. means that not need to write something to spi port before than read
129 #define SPI_AR_ON SPI_CFG_PORT |= (1 << _SPI_AREAD)
130 #define SPI_AR_OFF SPI_CFG_PORT &= ~(1 << _SPI_AREAD)
131
132 //spi chip select
133 #define SPI_SS_OFF SPI_CFG_PORT |= (1 << _SPI_SS)
134 #define SPI_SS_ON SPI_CFG_PORT &= ~(1 << _SPI_SS)
135
136 //spi speed. low speed need only for sd init
137 #define SPI_HI_SPEED_ON SPI_CFG_PORT |= (1 << _SPI_FULL_SPEED)
138 #define SPI_HI_SPEED_OFF SPI_CFG_PORT &= ~(1 << _SPI_FULL_SPEED)
139
140 //usb-serial port
141 #define FIFO_PORT *((volatile u16*) (0xA1300A))
142
143 //spi and usb state
144 #define STATE_PORT *((volatile u16*) (0xA1300C))
145 #define _STATE_SPI_READY 0
146 #define _STATE_FIFO_WR_READY 1
147 #define _STATE_FIFO_RD_READY 2
148 #define IS_FIFO_RD_READY (STATE_PORT & (1 << _STATE_FIFO_RD_READY))
149 #define IS_FIFO_WR_READY (STATE_PORT & (1 << _STATE_FIFO_WR_READY))
150
151
152 #endif  /* _EDOS_H */
153