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