Implement interrupts
[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 > 32767 then
8 set M to (join "0x" (item ((addr - 32768) mod 16384 + 1) of PRG-ROM))
9 end
10 end
11
12 define mapper write (addr) (value)
13 if addr < 8192 then
14 replace item addr mod 2048 + 1 of RAM with value
15 else
16 if addr > 8191 and addr < 8200 then
17 ask join join (value) " to PPU " (addr) and wait
18 else
19 if addr > 32768 then
20 say "Writing to ROM isn't very nice, you know..."
21 end
22 end
23 end
24
25 define PLP
26 set flagN to < (item (join (letter 1 of M) "8")+1 of AND) = 8>
27 set flagV to < (item (join (letter 1 of M) "4")+1 of AND) = 4>
28 set flagB to < (item (join (letter 1 of M) "1")+1 of AND) = 1>
29 set flagD to < (item (join (letter 2 of M) "8")+1 of AND) = 8>
30 set flagI to < (item (join (letter 2 of M) "4")+1 of AND) = 4>
31 set flagZ to < (item (join (letter 2 of M) "2")+1 of AND) = 2>
32 set flagC to < (item (join (letter 2 of M) "1")+1 of AND) = 1>
33
34 define interrupt: vector (vector)
35 mapper write 256 + S (PC - (PC mod 256))
36 set S to (S - 1) mod 256
37 mapper write 256 + S (PC mod 256)
38 set S to (S - 1) mod 256
39 compute SR
40 mapper write 256 + S SR
41 set S to (S - 1) mod 256
42 set flagI to 1
43 mapper read vector
44 set tmp to M
45 mapper read vector+1
46 set PC to (M*256) + tmp
47
48 define compute SR
49 set SR to (128*flagN) + (64*flagV) + (32) + (16*flagB) + (8*flagD) + (4*flagI) + (2*flagZ) + (1*flagC)
50
51 when flag clicked
52 set A to 0
53 set X to 0
54 set Y to 0
55 set S to "0xFD"
56 set PC to 49152
57 show variable A
58 show variable X
59 show variable Y
60 show variable PC
61 show variable M
62 show variable flagC
63 show variable flagV
64 show variable flagN
65 show variable flagZ
66 show variable flagI
67 show variable flagB
68 show variable flagD
69 show variable S
70 set line to 0
71 show variable line
72 delete all of RAM
73 repeat 2048
74 add "0" to RAM
75 end
76
77 when p key pressed
78 mapper read (PC)
79 interpret instruction (M)
80 change line by 1
81
82 define interpret instruction (opcode)
This page took 0.06525 seconds and 4 git commands to generate.