1 #include "lib/string.h"
11 static unsigned const char bootrom[256] =
13 0x31, 0xFE, 0xFF, 0xAF, 0x21, 0xFF, 0x9F, 0x32, 0xCB, 0x7C, 0x20, 0xFB, 0x21, 0x26, 0xFF, 0x0E,
14 0x11, 0x3E, 0x80, 0x32, 0xE2, 0x0C, 0x3E, 0xF3, 0xE2, 0x32, 0x3E, 0x77, 0x77, 0x3E, 0xFC, 0xE0,
15 0x47, 0x11, 0x04, 0x01, 0x21, 0x10, 0x80, 0x1A, 0xCD, 0x95, 0x00, 0xCD, 0x96, 0x00, 0x13, 0x7B,
16 0xFE, 0x34, 0x20, 0xF3, 0x11, 0xD8, 0x00, 0x06, 0x08, 0x1A, 0x13, 0x22, 0x23, 0x05, 0x20, 0xF9,
17 0x3E, 0x19, 0xEA, 0x10, 0x99, 0x21, 0x2F, 0x99, 0x0E, 0x0C, 0x3D, 0x28, 0x08, 0x32, 0x0D, 0x20,
18 0xF9, 0x2E, 0x0F, 0x18, 0xF3, 0x67, 0x3E, 0x64, 0x57, 0xE0, 0x42, 0x3E, 0x91, 0xE0, 0x40, 0x04,
19 0x1E, 0x02, 0x0E, 0x0C, 0xF0, 0x44, 0xFE, 0x90, 0x20, 0xFA, 0x0D, 0x20, 0xF7, 0x1D, 0x20, 0xF2,
20 0x0E, 0x13, 0x24, 0x7C, 0x1E, 0x83, 0xFE, 0x62, 0x28, 0x06, 0x1E, 0xC1, 0xFE, 0x64, 0x20, 0x06,
21 0x7B, 0xE2, 0x0C, 0x3E, 0x87, 0xE2, 0xF0, 0x42, 0x90, 0xE0, 0x42, 0x15, 0x20, 0xD2, 0x05, 0x20,
22 0x4F, 0x16, 0x20, 0x18, 0xCB, 0x4F, 0x06, 0x04, 0xC5, 0xCB, 0x11, 0x17, 0xC1, 0xCB, 0x11, 0x17,
23 0x05, 0x20, 0xF5, 0x22, 0x23, 0x22, 0x23, 0xC9, 0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0D, 0x00, 0x0B,
24 0x03, 0x73, 0x00, 0x83, 0x00, 0x0C, 0x00, 0x0D, 0x00, 0x08, 0x11, 0x1F, 0x88, 0x89, 0x00, 0x0E,
25 0xDC, 0xCC, 0x6E, 0xE6, 0xDD, 0xDD, 0xD9, 0x99, 0xBB, 0xBB, 0x67, 0x63, 0x6E, 0x0E, 0xEC, 0xCC,
26 0xDD, 0xDC, 0x99, 0x9F, 0xBB, 0xB9, 0x33, 0x3E, 0x3C, 0x42, 0xB9, 0xA5, 0xB9, 0xA5, 0x42, 0x3C,
27 0x21, 0x04, 0x01, 0x11, 0xA8, 0x00, 0x1A, 0x13, 0xBE, 0x20, 0xFE, 0x23, 0x7D, 0xFE, 0x34, 0x20,
28 0xF5, 0x06, 0x19, 0x78, 0x86, 0x23, 0x05, 0x20, 0xFB, 0x86, 0x20, 0xFE, 0x3E, 0x01, 0xE0, 0x50
32 static int bootrom_enabled = 0;
34 static int bootrom_enabled = 1;
37 static unsigned char *mem;
39 static unsigned int DMA_pending;
40 static unsigned short DMA_src;
41 static int DMA_copied;
43 static int joypad_select_buttons, joypad_select_directions;
45 void mem_bank_switch(
unsigned int n)
47 unsigned char *b = rom_getbytes();
49 if(!rom_bank_valid(n))
51 printf(
"Bank switch to illegal bank %d, ignoring.\n", n);
55 memcpy(&mem[0x4000], &b[n * 0x4000], 0x4000);
59 unsigned char mem_get_raw(
unsigned short p)
64 unsigned char mem_get_byte(
unsigned short i)
66 unsigned char mask = 0;
68 if(i < 0x100 && bootrom_enabled)
71 if(DMA_pending && cpu_get_cycles() >= DMA_pending)
73 unsigned long elapsed;
77 memcpy(&mem[0xFE00], &mem[DMA_src], 0xA0);
81 elapsed = cpu_get_cycles() - DMA_pending;
88 if(i >= 0xFE00 && i <= 0xFEA0)
92 if(!(((DMA_src >> 13) == 4) ^ ((i >> 13) == 4)))
93 return mem[0xFE00+elapsed];
100 if(!joypad_select_buttons)
101 mask = sdl_get_buttons();
102 if(!joypad_select_directions)
103 mask = sdl_get_directions();
104 return 0xC0 | (0xF^mask) | (joypad_select_buttons | joypad_select_directions);
107 return timer_get_div();
110 return timer_get_counter();
113 return timer_get_modulo();
116 return timer_get_tac();
119 return interrupt_get_IF();
122 return lcd_get_stat();
125 return lcd_get_line();
128 return lcd_get_ly_compare();
134 return interrupt_get_mask();
138 if(i >= 0xE000 && i <= 0xFDFF)
141 if(i >= 0x8000 && i <= 0x9FFF && (lcd_get_stat() & 3) == 3)
147 unsigned short mem_get_word(
unsigned short i)
149 return mem_get_byte(i) | (mem_get_byte(i+1)<<8);
152 void mem_write_byte(
unsigned short d,
unsigned char i)
154 unsigned int filtered = 0;
156 if(DMA_pending && DMA_pending <= cpu_get_cycles())
162 memcpy(&mem[0xFE00], &mem[DMA_src], 0xA0);
166 elapsed = cpu_get_cycles() - DMA_pending;
170 else if(d >= 0xFE00 && d <= 0xFEA0)
173 else if(!(((DMA_src >> 13) == 4) ^ ((d >> 13) == 4)))
177 switch(rom_get_mapper())
186 filtered = MBC3_write_byte(d, i);
189 filtered = MBC1_write_byte(d, i);
199 joypad_select_buttons = i&0x20;
200 joypad_select_directions = i&0x10;
208 timer_set_counter(i);
220 lcd_write_control(i);
226 lcd_write_scroll_y(i);
229 lcd_write_scroll_x(i);
232 lcd_set_ly_compare(i);
236 if(DMA_pending == cpu_get_cycles())
240 DMA_pending = cpu_get_cycles()+2;
245 lcd_write_bg_palette(i);
248 lcd_write_spr_palette1(i);
251 lcd_write_spr_palette2(i);
263 interrupt_set_mask(i);
269 if(d > 0x8000 && d < 0x9FFF && (lcd_get_stat() & 3) == 3)
274 if(d >= 0xE000 && d <= 0xFDFF)
280 void mem_write_word(
unsigned short d,
unsigned short i)
282 mem_write_byte(d, i&0xFF);
283 mem_write_byte(d+1, i>>8);
288 unsigned char *bytes = rom_getbytes();
290 mem = calloc(1, 0x10000);
292 memcpy(&mem[0x0000], &bytes[0x0000], 0x4000);
293 memcpy(&mem[0x4000], &bytes[0x4000], 0x4000);
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.