Skip to content

Commit

Permalink
Fixed opcodes address
Browse files Browse the repository at this point in the history
Added opcodes instruction group
  • Loading branch information
lmmendes committed Dec 8, 2020
1 parent 01c389d commit fbdd65b
Show file tree
Hide file tree
Showing 4 changed files with 638 additions and 83 deletions.
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
## [Version 1.0.1](https://github.com/lmmendes/game-boy-opcodes/releases/tag/v1.0.1) (2019-04-20)
## 1.1.0

- Fix instruction timings for LD (C),A and LD A,(C) and STOP: [`merge request 1`](https://github.com/lmmendes/game-boy-opcodes/pull/1/files/0db27cd5f98e273ff7e0ad08fada43bd9e79d632)
* Fixed a huge bug with the opcode addresses
* Added opcode group

## [Version 1.0.0](https://github.com/lmmendes/game-boy-opcodes/releases/tag/v1.0.0) (2018-03-19)
## 1.0.1

- Initial release.
* Fix instruction timings for LD (C),A and LD A,(C) and STOP (#1)

## 1.0.0

* Initial release.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Game Boy CPU (Sharp LR35902) instruction set (opcodes)

The [opcodes.json](https://raw.githubusercontent.com/lmmendes/game-boy-opcodes/master/opcodes.json) contains a JSON representation of the complete Sharp LR35902 instruction set. Inside the `bin` folder you will find the script used to generate the `opcodes.json` file from the pastraiser.com site.

## Description of opcodes.json

> The opcodes.json file includes some minor fixes in the opcode length and timings not present on the pastraiser.com site.
```
Expand All @@ -22,6 +24,7 @@ The [opcodes.json](https://raw.githubusercontent.com/lmmendes/game-boy-opcodes/m
"-" <-- C - Carry Flag
],
"addr": "0xc0", <-- Address
"group": "control/misc" <-- Opcode group
"operand1": "0", <-- Operand 1
"operand2": "B" <-- Operand 2
},
Expand All @@ -31,6 +34,20 @@ The [opcodes.json](https://raw.githubusercontent.com/lmmendes/game-boy-opcodes/m
}
```

Operation group table:

|op group |description|
|-----------------|-----------------------------|
|x8/lsm |8-bit Load/Store/Move|
|x16/lsm |16-bit Load/Store/Move|
|x8/alu |8-bit Arithmetic Logic Unit|
|x16/alu |16-bit Arithmetic Logic Unit|
|x8/rsb |8-bit ???|
|control/br |branch|
|control/misc |misc|



## Reference documentation

http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html
Expand Down
40 changes: 36 additions & 4 deletions bin/opcodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@
end
end

def table_extractor(table)
COLOR_GROUP_TABLE = {
"#ccccff" => "x8/lsm",
"#ccffcc" => "x16/lsm",
"#ffff99" => "x8/alu",
"#ffcccc" => "x16/alu",
"#80ffff" => "x8/rsb",
"#ffcc99" => "control/br",
"#ff99cc" => "control/misc"
}

PATCH_OPCODES = {
"unprefixed" =>{
"0x10" => {
"length" => 1
},
"0xe2" => {
"length" => 1
},
"0xf2" => {
"length" => 1
}
}
}

def table_extractor(table, table_name)
opcodes = {}
op_index = 0
table.css('tr').each_with_index do |tr, tr_index|
Expand All @@ -30,17 +54,25 @@ def table_extractor(table)
operand1, operand2 = operators.to_s.split(',')
length, cycles = td.children[2].text.split(/[[:space:]]+/)
flags = td.children[4].text.split(/[[:space:]]+/)
addr = "0x#{op_index.to_s(16)}"
addr = "0x#{(tr_index-1).to_s(16)}#{(td_index-1).to_s(16)}"
opcolor = td["bgcolor"]
opcodes[ addr ] = {
"mnemonic" => opcode,
"length" => length.to_i,
"cycles" => cycles.split('/').collect(&:to_i),
"flags" => flags,
"addr" => addr
}
opcodes[ addr ]["group"] = COLOR_GROUP_TABLE[opcolor] if COLOR_GROUP_TABLE[opcolor]
opcodes[ addr ]["operand1"] = operand1.to_s if operand1
opcodes[ addr ]["operand2"] = operand2.to_s if operand2
op_index+=1

# apply patches id needed
if PATCH_OPCODES[table_name] && PATCH_OPCODES[table_name][addr]
puts "patching #{addr} on #{table_name}"
opcodes[ addr ] = opcodes[ addr ].merge(PATCH_OPCODES[table_name][addr])
end
end
end
opcodes
Expand All @@ -50,8 +82,8 @@ def table_extractor(table)
html = Oga.parse_html File.open('opcodes.html').read
tables = html.css('table')
opcodes = {
'unprefixed' => table_extractor(tables[0]),
'cbprefixed' => table_extractor(tables[1])
'unprefixed' => table_extractor(tables[0], "unprefixed"),
'cbprefixed' => table_extractor(tables[1], "cbprefixed")
}

File.open('opcodes.json', 'w').write JSON.pretty_generate(opcodes)
Loading

0 comments on commit fbdd65b

Please sign in to comment.