FileReader/BufferedReader are intended for reading text
files, and therefore they are going to mangle the data and give you incorrect results. You do not want to use them for reading a ROM. You want to use something designed for reading binary
files. I'm not familiar enough with Java to know exactly which class to use, but maybe something like BinaryFileReader or something?
However, some of the opcodes I'm getting are only one byte instead of two.
is exactly 1 byte. Always.
consists of an opcode byte, followed by 0-2 additional bytes of data for the operand. How many bytes follow the opcode depends on the opcode itself.
For a disassembler, you'll want to read 1 byte as the opcode, check which opcode it is to see how many bytes are supposed to follow it.
Example, if you are given the below bytes:
A9 00 18 6D 31 03 85 80
It should disassemble to the below:
LDA #$00 ; A9 00 'A9' opcode is "LDA immediate".. so 1 byte follows the opcode (2 bytes total)
CLC ; 18 '18' opcode is "CLC implied".. so 0 bytes after opcode (1 byte total)
ADC $0331 ; 6D 31 03 '6D' opcode is "ADC absolute".. so 2 bytes following (3 total)
STA $80 ; 85 80 '85' opcode is "STA zero page".. so 1 byte following (2 total)
A reference for all opcodes and what they mean can be found here:http://www.obelisk.demon.co.uk/6502/reference.html
To check to make sure you are reading bytes properly from the file -- get a hex editor and look at the file
. A hex editor allows you to view every single byte of the file in raw binary form. It's invaluable to doing any kind of binary work because you can see the exact layout and know exactly what data you're dealing with.
HxD is my favorite free hex editor:http://mh-nexus.de/en/hxd/
Again -- when programming, you do not
want to read the file as text. You want to read it as binary data.