Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit 723ec96

Browse files
author
moatom
committed
Finish debugging the basic CPU implementation , perhaps
1 parent 5e97da2 commit 723ec96

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

cpu.sml

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,28 @@ datatype operand =
7979
| Address of address
8080

8181

82-
fun zpRead16 addr =
83-
Word16.orb (w8ToW16 (rd (wram, addr)), Word16.<< (w8ToW16 (rd (wram, (addr+0wx1) mod 0wx100 )), 0wx8))
82+
fun samePageRead16 addr =
83+
Word16.orb (w8ToW16 (rd (wram, addr)), Word16.<< (w8ToW16 (rd (wram, Word16.orb (Word16.andb (addr, 0wxff00), (addr+0wx1) mod 0wx100))), 0wx8))
8484

8585
exception InvaildAccess
8686
(* val address : registers * address -> word16 *)
8787
fun address (regs, ZeroPage x) = w8ToW16 x
8888
| address (regs, Absolute x) = x
89-
| address (regs, Relative x) = (#PC regs) + w8ToW16 x
90-
| address (regs, Indirect x) = read16 x(* w8ToW16 (read x) *)
89+
| address (regs, Relative x) = (#PC regs) + w8ToW16 x - (if x >= 0w128 then 0w256
90+
else 0w0)
91+
| address (regs, Indirect x) = samePageRead16 x
9192
| address (regs, ZeroPageIndexedX x) = ((w8ToW16 x) + w8ToW16 (#X regs)) mod 0wx100
9293
| address (regs, ZeroPageIndexedY x) = ((w8ToW16 x) + w8ToW16 (#Y regs)) mod 0wx100
9394
| address (regs, AbsoluteIndexedX x) = x + w8ToW16 (#X regs)
9495
| address (regs, AbsoluteIndexedY x) = x + w8ToW16 (#Y regs)
95-
| address (regs, IndexedIndirect x) = zpRead16 ((w8ToW16 x + w8ToW16 (#X regs)) mod 0wx100)
96-
| address (regs, IndirectIndexed x) = (zpRead16 (w8ToW16 x)) + w8ToW16 (#Y regs)
96+
| address (regs, IndexedIndirect x) = samePageRead16 ((w8ToW16 x + w8ToW16 (#X regs)) mod 0wx100)
97+
| address (regs, IndirectIndexed x) = (samePageRead16 (w8ToW16 x)) + w8ToW16 (#Y regs)
9798
| address _ = raise InvaildAccess
9899

99100
fun value (regs, Immediate x) = x
100101
| value (regs, Address a) = read (address (regs, a))
101102

103+
102104
(* valueしたいかどうか *)
103105
datatype instruction =
104106
ADC of operand
@@ -134,7 +136,7 @@ datatype instruction =
134136
| LDX of operand
135137
| LDY of operand
136138
| LSR of operand
137-
| NOP of address
139+
| NOP of operand
138140
| ORA of operand
139141
| PHA of address
140142
| PHP of address
@@ -401,7 +403,7 @@ fun fetchAndDecode (pc: word16): instruction * word16 =
401403
| 0wxE6 => (INC (Address (zp ())), pc + 0w2)
402404
| 0wxE8 => (INX Implied, pc + 0w1)
403405
| 0wxE9 => (SBC (imm ()), pc + 0w2)
404-
| 0wxEA => (NOP Implied, pc + 0w1)
406+
| 0wxEA => (NOP (Address Implied), pc + 0w1)
405407
| 0wxEC => (CPX (Address (abs ())), pc + 0w3)
406408
| 0wxED => (SBC (Address (abs ())), pc + 0w3)
407409
| 0wxEE => (INC (Address (abs ())), pc + 0w3)
@@ -412,7 +414,31 @@ fun fetchAndDecode (pc: word16): instruction * word16 =
412414
| 0wxF8 => (SED Implied, pc + 0w1)
413415
| 0wxF9 => (SBC (Address (aby ())), pc + 0w3)
414416
| 0wxFD => (SBC (Address (abx ())), pc + 0w3)
415-
| 0wxFE => (INC (Address (abx ())), pc + 0w3)
417+
| 0wxFE => (INC (Address (abx ())), pc + 0w3)
418+
(* some illegal opcodes for an test *)
419+
| 0wx04 => (NOP (Address (zp())), pc + 0w2)
420+
| 0wx44 => (NOP (Address (zp())), pc + 0w2)
421+
| 0wx64 => (NOP (Address (zp())), pc + 0w2)
422+
| 0wx0C => (NOP (Address (abs())), pc + 0w3)
423+
| 0wx14 => (NOP (Address (zpx())), pc + 0w2)
424+
| 0wx34 => (NOP (Address (zpx())), pc + 0w2)
425+
| 0wx54 => (NOP (Address (zpx())), pc + 0w2)
426+
| 0wx74 => (NOP (Address (zpx())), pc + 0w2)
427+
| 0wxD4 => (NOP (Address (zpx())), pc + 0w2)
428+
| 0wxF4 => (NOP (Address (zpx())), pc + 0w2)
429+
| 0wx1A => (NOP (Address Implied), pc + 0w1)
430+
| 0wx3A => (NOP (Address Implied), pc + 0w1)
431+
| 0wx5A => (NOP (Address Implied), pc + 0w1)
432+
| 0wx7A => (NOP (Address Implied), pc + 0w1)
433+
| 0wxDA => (NOP (Address Implied), pc + 0w1)
434+
| 0wxFA => (NOP (Address Implied), pc + 0w1)
435+
| 0wx80 => (NOP (imm ()), pc + 0w2)
436+
| 0wx1C => (NOP (Address (abx ())), pc + 0w3)
437+
| 0wx3C => (NOP (Address (abx ())), pc + 0w3)
438+
| 0wx5C => (NOP (Address (abx ())), pc + 0w3)
439+
| 0wx7C => (NOP (Address (abx ())), pc + 0w3)
440+
| 0wxDC => (NOP (Address (abx ())), pc + 0w3)
441+
| 0wxFC => (NOP (Address (abx ())), pc + 0w3)
416442
| _ => (print (Word16.toString pc ^ ":"
417443
^ Word8.toString (read pc) ^ " "
418444
^ Word8.toString (read (0w1 + pc)) ^ " "

0 commit comments

Comments
 (0)