Skip to content

Commit 2397c60

Browse files
committed
add configuration for disassemble all addresses (not just even addresses)
1 parent 02989b7 commit 2397c60

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

pkg/chip8/chip8.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const fontAddressDefault = 0x050
1313
const flagRegisterIndex = 0xF
1414

1515
type Configuration struct {
16-
Disassemble bool // Disassemble do execute the ROM program but rather prints it to stdout with, more or less, natural language explanation to each instruction
1716
Debug bool // Debug mode prints, in more or less natural language, the instructions performed during the program execution
1817
ModeRomCompatibility bool // ModeRomCompatibility The preferred mode setting for most ROM compatibility
1918
ModeStrictCosmac bool // ModeStrictCosmac infers strict original instruction execution as COSMAC was designed (far from all ROM adhere to this)
2019
EndOnInfiniteLoop bool // EndOnInfiniteLoop ends the program if an infinite loop is detected (some program ends with infinite loop and require restart to run again)
2120
RestartOnInfiniteLoop bool // RestartOnInfiniteLoop restarts the program if an infinite loop is detected (some program ends with infinite loop and require restart to run again)
21+
22+
Disassemble bool // Disassemble do execute the ROM program but rather prints it to stdout with, more or less, natural language explanation to each instruction
23+
DisassembleEveryByte bool // DisassembleEveryByte try to disassemble instructions at all bytes not just at even addresses. Some programs have parts of the code based at uneven addresses.
2224
}
2325

2426
type Chip8 struct {

pkg/chip8/debug.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ var instructionRegExp = map[string]*regexp.Regexp{
4848
func DisassembleProgram(romFilepath string, startAddress uint16, configuration Configuration) {
4949
bytes := loadByteFile(romFilepath)
5050

51-
for address := uint16(0); address < uint16(len(bytes)); address++ {
51+
for address := uint16(0); address < (uint16(len(bytes)) - 1); address++ {
5252

5353
binaryBitsText := strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%08b", bytes[address]), "0", "░"), "1", "█")
54-
if (address % 2) == 0 {
54+
if (address%2) == 0 || configuration.DisassembleEveryByte {
5555
instructionCode := uint16(bytes[address+0])<<8 | uint16(bytes[address+1])
5656

5757
if matchesAnyInstructionSyntax(instructionCode) {
@@ -191,7 +191,7 @@ func explanation(instruction uint16, configuration Configuration) string {
191191
if configuration.ModeStrictCosmac || configuration.ModeRomCompatibility {
192192
if instructionRegExp["BNNN"].MatchString(instructionText) {
193193
matches := instructionRegExp["BNNN"].FindStringSubmatch(instructionText)
194-
return fmt.Sprintf("BNNN: Jump to address 0x%s%s plus offset found in register V0", matches[1], matches[2])
194+
return fmt.Sprintf("BNNN: Jump to address 0x%s%s plus offset found in register V0", matches[1])
195195
}
196196
} else {
197197
if instructionRegExp["BXNN"].MatchString(instructionText) {
1.27 KB
Binary file not shown.

0 commit comments

Comments
 (0)