-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrom_file.rb
More file actions
executable file
·82 lines (68 loc) · 2.65 KB
/
rom_file.rb
File metadata and controls
executable file
·82 lines (68 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require "constants"
class ROMFile
attr_reader :prg_page_count, :chr_page_count, :mapper
attr_reader :rom_control_1, :rom_control_2
attr_reader :prg_rom_pages, :chr_rom_pages
attr_reader :mirroring
include Constants
def initialize(rom_file_path)
super()
@prg_page_count = 0
@chr_page_count = 0
@mapper = 0
@mirroring = 0
@rom_control_1 = 0
@rom_control_2 = 0
@prg_rom_pages = nil
@chr_rom_pages = nil
load_rom_file(rom_file_path)
end
def load_rom_file(rom_file_path)
begin
fd = IO.sysopen(rom_file_path, "rb")
rescue
DEBUG.debug_print "Error opening ROM file: #{rom_file_path}\n"
DEBUG.debug_getcommands
raise
end
IO.open(fd) { |io|
filetype = io.read(4) # Should be "NES0x1A"
@prg_page_count = io.readchar # 16k PRG-ROM Page Count
@chr_page_count = io.readchar # 8k CHR-ROM Page Count
@rom_control_1 = io.readchar
@rom_control_2 = io.readchar
@mapper = (@rom_control_1 >> 4) | @rom_control_2
@mirroring |= (@rom_control_1 & 0x08) >> 2
@mirroring = @rom_control_1 & 0x01 if @mirroring == 0
io.read(8) # Read in 8 remaining header bytes (unused at the moment)
DEBUG.debug_print "File Type: " + filetype.to_s + "\n"
DEBUG.debug_print "PRG-ROM Pages: " + @prg_page_count.to_s + "\n"
DEBUG.debug_print "CHR-ROM Pages: " + @chr_page_count.to_s + "\n"
DEBUG.debug_print "ROM Control Byte #1: " + @rom_control_1.to_s + "\n"
DEBUG.debug_print "ROM Control Byte #2: " + @rom_control_2.to_s + "\n"
DEBUG.debug_print "Mapper: " + @mapper.to_s + "\n"
if @mirroring == 2
DEBUG.debug_print "Mirroring: Four Screen\n"
else
DEBUG.debug_print "Mirroring: #{@mirroring == 0 ? 'Horizontal' : 'Vertical'}\n"
end
# Load PRG-ROM and CHR-Pages into object variables
@prg_rom_pages = Array.new(@prg_page_count)
@chr_rom_pages = Array.new(@chr_page_count)
# Read in data for PRG-ROM pages (@TODO: there's got to be a cleaner way!)
(0...@prg_page_count).each { |i|
@prg_rom_pages[i] = Array.new(PRG_ROM_PAGE_SIZE,0)
(0...PRG_ROM_PAGE_SIZE).each { |j|
@prg_rom_pages[i][j] = io.readchar
}
}
# Read in data for CHR-ROM pages (@TODO: there's got to be a cleaner way!)
(0...@chr_page_count).each { |i|
@chr_rom_pages[i] = Array.new(CHR_ROM_PAGE_SIZE,0)
(0...CHR_ROM_PAGE_SIZE).each { |j|
@chr_rom_pages[i][j] = io.readchar
}
}
}
end
end