276af55cc6f2f849be88c02c33287807f81e9c9c
[ScratchNES.git] / src / CPU / common.tosh
1 define ; [comment]
2
3 define-atomic mapper read (addr)
4 if addr > 32767 then
5 set M to (join "0x" (item ((addr - 32768) mod 16384 + 1) of PRG-ROM))
6 else
7 if addr < 8192 then
8 set M to item (addr mod 2048 + 1) of RAM
9 else
10 if addr > 8192 and addr < 8200 then
11 read PPU register (addr - 8192)
12 else
13 if addr = 16405 then
14 read controller 1
15 end
16 end
17 end
18 end
19
20 define-atomic mapper write (addr) (value)
21 if addr < 8192 then
22 replace item addr mod 2048 + 1 of RAM with value
23 else
24 if addr > 8191 and addr < 8200 then
25 write PPU register (addr - 8192) value: (value)
26 else
27 if addr = 16405 then
28 controller strobe (value)
29 else
30 if addr = 16404 then
31 OAM DMA (value)
32 else
33 if addr > 32768 then
34 say "Writing to ROM isn't very nice, you know..."
35 end
36 end
37 end
38 end
39 end
40
41 define-atomic PLP
42 set flagN to < (item (join (letter 1 of M) "8")+1 of AND) = 8>
43 set flagV to < (item (join (letter 1 of M) "4")+1 of AND) = 4>
44 set flagB to < (item (join (letter 1 of M) "1")+1 of AND) = 1>
45 set flagD to < (item (join (letter 2 of M) "8")+1 of AND) = 8>
46 set flagI to < (item (join (letter 2 of M) "4")+1 of AND) = 4>
47 set flagZ to < (item (join (letter 2 of M) "2")+1 of AND) = 2>
48 set flagC to < (item (join (letter 2 of M) "1")+1 of AND) = 1>
49
50 define-atomic interrupt: vector [vector]
51 mapper write 256 + S (((PC - (PC mod 256)))/256)
52 set S to (S - 1) mod 256
53 mapper write 256 + S (PC mod 256)
54 set S to (S - 1) mod 256
55 set SR to (128*flagN) + (64*flagV) + (32) + (16*flagB) + (8*flagD) + (4*flagI) + (2*flagZ) + (1*flagC)
56 mapper write 256 + S SR
57 set S to (S - 1) mod 256
58 set flagI to 1
59 mapper read vector
60 set tmp to M
61 mapper read vector+1
62 set PC to (M*256) + tmp
63
64 when flag clicked
65 set A to 0
66 set X to 0
67 set Y to 0
68 set S to "0xFD"
69 set PC to 49152
70 show variable PC
71 set line to 0
72 show variable line
73 delete all of RAM
74 repeat 2048
75 add "0" to RAM
76 end
77 initialize PPU
78
79 when p key pressed
80 step CPU
81
82 when f key pressed
83 forever
84 emulate frame
85 end
86
87 define-atomic step CPU
88 mapper read (PC)
89 set opcode to M
90 change line by 1
This page took 0.074466 seconds and 3 git commands to generate.