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