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