-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
534 lines (423 loc) · 14.4 KB
/
main.rb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
require "rubygems"
gem "ruby-processing"
require "ruby-processing"
require "nes"
require "debugger"
require "palette"
require "constants"
require "socket"
include_class "javax.swing.JFileChooser"
include_class "java.lang.System"
module Processing
SKETCH_PATH = "./"
end
class Main < Processing::App
attr_accessor :nes
include Palette
include Constants
# Game Canvas Area
CANVAS_X=100
CANVAS_Y=0
CANVAS_W=256
CANVAS_H=241
# Buttons
BTN_PANEL_W=100
BTN_PANEL_H=300
CHECKBOX_LABEL_FONT="Monospaced.bold"
CHECKBOX_LABEL_SIZE=13
CHECKBOX_UNCHECKED_IMG="icons/checkbox-unchecked.jpg"
CHECKBOX_CHECKED_IMG="icons/checkbox-checked.jpg"
LOAD_BTN_X=5
LOAD_BTN_Y=10
LOAD_BTN_H=90
LOAD_BTN_W=90
LOAD_BTN_IMG="icons/load.jpg"
PWR_BTN_X=10
PWR_BTN_Y=105
PWR_BTN_H=40
PWR_BTN_W=80
PWR_BTN_IMG="icons/power.jpg"
RESET_BTN_X=10
RESET_BTN_Y=150
RESET_BTN_H=40
RESET_BTN_W=80
RESET_BTN_IMG="icons/reset.jpg"
LOGO_X=101
LOGO_Y=250
LOGO_W=256
LOGO_H=40
LOGO_IMG="icons/logo.jpg"
BOTTOM_PANEL_X=100
BOTTOM_PANEL_Y=241
BOTTOM_PANEL_W=256
BOTTOM_PANEL_H=59
DEBUG_LABEL="Debug?"
DEBUG_LABEL_X=5
DEBUG_LABEL_Y=215
DEBUG_BTN_X=78
DEBUG_BTN_Y=200
DEBUG_BTN_W=20
DEBUG_BTN_H=20
PALETTE_LABEL="Show \nPalette?"
PALETTE_LABEL_X=5
PALETTE_LABEL_Y=240
PALETTE_BTN_X=78
PALETTE_BTN_Y=232
PALETTE_BTN_W=20
PALETTE_BTN_H=20
PTABLE_LABEL="Show \nP.Tables?"
PTABLE_LABEL_X=5
PTABLE_LABEL_Y=275
PTABLE_BTN_X=78
PTABLE_BTN_Y=270
PTABLE_BTN_W=20
PTABLE_BTN_H=20
# Palette Viewer Canvas Area
PALETTE_VIEWER_X=101
PALETTE_VIEWER_Y=250
PALETTE_VIEWER_W=256
PALETTE_VIEWER_H=32
# Pattern Table Canvas Area
PATTERN_TABLE_VIEWER_X=101
PATTERN_TABLE_VIEWER_Y=0
PATTERN_TABLE_VIEWER_W=128
PATTERN_TABLE_VIEWER_H=256
def initialize(file)
super()
@file = file
end
def setup
@title = "RubyNES"
@palette_viewer_shown = false
@palette_viewer = nil
@pattern_table_viewer_shown = false
@pattern_table_viewer = nil
@paused = false
size 356, 300, P2D
# Bottom Panel
fill 0xFF, 0xFF, 0xFF
rect BOTTOM_PANEL_X, BOTTOM_PANEL_Y, BOTTOM_PANEL_W, BOTTOM_PANEL_H
# Control Panel
fill 0xAA, 0xAA, 0xAA
rect 0, 0, BTN_PANEL_W, BTN_PANEL_H
# Logo
logo = loadImage LOGO_IMG
image logo, LOGO_X, LOGO_Y, LOGO_W, LOGO_H
# Load Buton
load = loadImage LOAD_BTN_IMG
image load, LOAD_BTN_X, LOAD_BTN_Y, LOAD_BTN_W, LOAD_BTN_H
# Power Button
power = loadImage PWR_BTN_IMG
image power, PWR_BTN_X, PWR_BTN_Y, PWR_BTN_W, PWR_BTN_H
# Reset Button
reset = loadImage RESET_BTN_IMG
image reset, RESET_BTN_X, RESET_BTN_Y, RESET_BTN_W, RESET_BTN_H
# List all fonts
#fontList = PFont.list
#fontList.each {|font| STDOUT.puts "\n" + font}
checkbox_font = createFont CHECKBOX_LABEL_FONT, CHECKBOX_LABEL_SIZE
# Debugging Checkbox
debug = loadImage DEBUG.is_debugging? ? CHECKBOX_CHECKED_IMG : CHECKBOX_UNCHECKED_IMG
image debug, DEBUG_BTN_X, DEBUG_BTN_Y, DEBUG_BTN_W, DEBUG_BTN_H
fill 0, 0, 0
textFont checkbox_font
text DEBUG_LABEL, DEBUG_LABEL_X, DEBUG_LABEL_Y
# Palette Viewer Checkbox
palette = loadImage @palette_viewer_shown ? CHECKBOX_CHECKED_IMG : CHECKBOX_UNCHECKED_IMG
image palette, PALETTE_BTN_X, PALETTE_BTN_Y, PALETTE_BTN_W, PALETTE_BTN_H
fill 0, 0, 0
textFont checkbox_font
text PALETTE_LABEL, PALETTE_LABEL_X, PALETTE_LABEL_Y
# Pattern Table Viewer Checkbox
ptable = loadImage @pattern_table_viewer_shown ? CHECKBOX_CHECKED_IMG : CHECKBOX_UNCHECKED_IMG
image ptable, PTABLE_BTN_X, PTABLE_BTN_Y, PTABLE_BTN_W, PTABLE_BTN_H
fill 0, 0, 0
textFont checkbox_font
text PTABLE_LABEL, PTABLE_LABEL_X, PTABLE_LABEL_Y
# Now initialize the actual NES emulator
@nes = NES.new
# If a rom file was passed in on the command line, use it
if [email protected]? and [email protected]?
MAIN.frame.setTitle "RubyNES (#{@file})"
MAIN.nes.load_rom @file
end
# NTSC refresh rate (Should Be) 1/30 of a second
frameRate 30
end
def pause
@paused = true
update_titlebar_status "* Paused *"
end
def unpause
@paused = false
update_titlebar_status "Running"
end
def draw
if (@pattern_table_viewer_shown)
pt_img = createImage(PATTERN_TABLE_VIEWER_W, PATTERN_TABLE_VIEWER_H, RGB)
@pattern_table_viewer.repaint(pt_img)
image pt_img, PATTERN_TABLE_VIEWER_X, PATTERN_TABLE_VIEWER_Y, PATTERN_TABLE_VIEWER_W, PATTERN_TABLE_VIEWER_H
else
if @nes.is_power_on? and not @paused
@nes.run_one_frame
repaint
if @palette_viewer_shown
pal_img = createImage(PALETTE_VIEWER_W, PALETTE_VIEWER_H, RGB)
@palette_viewer.repaint(pal_img)
image pal_img, PALETTE_VIEWER_X, PALETTE_VIEWER_Y, PALETTE_VIEWER_W, PALETTE_VIEWER_H
end
end
end
end
def repaint
ppu = @nes.ppu
img = createImage(CANVAS_W, CANVAS_H, RGB)
img.loadPixels
ppu.screen_buffer.each_index { |scanline_index|
if (scanline_index != 0) # Scanline 1 in the ppu is a dummy scanline (nothing drawn)
scanline = ppu.screen_buffer[scanline_index]
scanline.each_index { |pixel_index|
pixel = COLORS[scanline[pixel_index]]
img.pixels[((scanline_index - 1) * 256) + pixel_index] = color(((pixel & 0xFF0000) >> 16), ((pixel & 0xFF00) >> 8), (pixel & 0xFF)) #
}
end
}
img.updatePixels
image img,CANVAS_X, CANVAS_Y, CANVAS_W, CANVAS_H
end
def mousePressed
x = mouseX
y = mouseY
load_button_handler x, y
power_button_handler x, y
reset_button_handler x, y
debug_button_handler x, y
palette_button_handler x, y
pattern_table_button_handler x, y
end
def keyPressed
mmc = @nes.mmc
# @TODO: Put in handling for second joystick, enable user key selection
if (key.respond_to? :chop and ["A","S","Z","X"].include?(key.upcase))
case key.upcase
when "A"
mmc.joystick_1_keys |= JOYSTICK_B
when "S"
mmc.joystick_1_keys |= JOYSTICK_A
when "Z"
mmc.joystick_1_keys |= JOYSTICK_SELECT
when "X"
mmc.joystick_1_keys |= JOYSTICK_START
end
elsif (key == CODED)
case keyCode
when UP
mmc.joystick_1_keys |= JOYSTICK_UP
when DOWN
mmc.joystick_1_keys |= JOYSTICK_DOWN
when LEFT
mmc.joystick_1_keys |= JOYSTICK_LEFT
when RIGHT
mmc.joystick_1_keys |= JOYSTICK_RIGHT
end
end
end
def load_button_handler(x, y)
return unless (LOAD_BTN_X..(LOAD_BTN_X + LOAD_BTN_W)).include? x
return unless (LOAD_BTN_Y..(LOAD_BTN_Y + LOAD_BTN_H)).include? y
c = JFileChooser.new(java.io.File.new(Dir.pwd))
Thread.new {
rVal = c.showOpenDialog(MAIN.frame)
if (rVal == JFileChooser::APPROVE_OPTION)
rom_file = c.getSelectedFile # Returns a Java File
@file = rom_file.getAbsolutePath
if @file != nil and not @file.empty?
MAIN.frame.setTitle "RubyNES (#{rom_file.getName})"
MAIN.nes.load_rom @file
end
end
}
end
def power_button_handler(x, y)
return unless (PWR_BTN_X..(PWR_BTN_X + PWR_BTN_W)).include? x
return unless (PWR_BTN_Y..(PWR_BTN_Y + PWR_BTN_H)).include? y
Thread.new {
MAIN.nes.power_on if MAIN.nes.rom_file_path != nil and not MAIN.nes.rom_file_path.empty?
}
update_titlebar_status "Running"
end
def reset_button_handler(x, y)
return unless (RESET_BTN_X..(RESET_BTN_X + RESET_BTN_W)).include? x
return unless (RESET_BTN_Y..(RESET_BTN_Y + RESET_BTN_H)).include? y
update_titlebar_status "Running"
Thread.new {
MAIN.nes.cpu.reset
}
end
def debug_button_handler(x, y)
return unless (DEBUG_BTN_X..(DEBUG_BTN_X + DEBUG_BTN_W)).include? x
return unless (DEBUG_BTN_Y..(DEBUG_BTN_Y + DEBUG_BTN_H)).include? y
if not DEBUG.is_debugging?
DEBUG.enable_debugging
debug = loadImage CHECKBOX_CHECKED_IMG
image debug, DEBUG_BTN_X, DEBUG_BTN_Y, DEBUG_BTN_W, DEBUG_BTN_H
else
DEBUG.disable_debugging
debug = loadImage CHECKBOX_UNCHECKED_IMG
image debug, DEBUG_BTN_X, DEBUG_BTN_Y, DEBUG_BTN_W, DEBUG_BTN_H
end
end
def palette_button_handler(x, y)
return unless (PALETTE_BTN_X..(PALETTE_BTN_X + PALETTE_BTN_W)).include? x
return unless (PALETTE_BTN_Y..(PALETTE_BTN_Y + PALETTE_BTN_H)).include? y
if not @palette_viewer_shown
@palette_viewer_shown = true
@palette_viewer = PaletteViewer.new(@nes)
palette = loadImage CHECKBOX_CHECKED_IMG
image palette, PALETTE_BTN_X, PALETTE_BTN_Y, PALETTE_BTN_W, PALETTE_BTN_H
else
@palette_viewer_shown = false
@palette_viewer = nil
palette = loadImage CHECKBOX_UNCHECKED_IMG
image palette, PALETTE_BTN_X, PALETTE_BTN_Y, PALETTE_BTN_W, PALETTE_BTN_H
# Replace the Logo
logo = loadImage LOGO_IMG
image logo, LOGO_X, LOGO_Y, LOGO_W, LOGO_H
end
end
def pattern_table_button_handler(x, y)
return unless (PTABLE_BTN_X..(PTABLE_BTN_X + PTABLE_BTN_W)).include? x
return unless (PTABLE_BTN_Y..(PTABLE_BTN_Y + PTABLE_BTN_H)).include? y
if (@nes.is_power_on?)
if not @pattern_table_viewer_shown
pause
@pattern_table_viewer_shown = true
@pattern_table_viewer = PatternTablesViewer.new(@nes)
ptable = loadImage CHECKBOX_CHECKED_IMG
image ptable, PTABLE_BTN_X, PTABLE_BTN_Y, PTABLE_BTN_W, PTABLE_BTN_H
else
unpause
@pattern_table_viewer_shown = false
@pattern_table_viewer = nil
ptable = loadImage CHECKBOX_UNCHECKED_IMG
image ptable, PTABLE_BTN_X, PTABLE_BTN_Y, PTABLE_BTN_W, PTABLE_BTN_H
end
end
end
def update_titlebar_status(status)
MAIN.frame.setTitle(MAIN.frame.getTitle.split(" - ")[0] + " - " + status)
end
end
# Class to enable the palette viewer functionality
class PaletteViewer
include Palette
include Constants
def initialize(nes)
super()
@nes = nes
end
def repaint(img)
ppu = @nes.ppu
mmc = ppu.mmc
palette = Array.new(32, 0)
(IMAGE_PALETTE_LO..SPRITE_PALETTE_HI).each {|address| palette[address - IMAGE_PALETTE_LO] = mmc.read_ppu_mem(address) }
colors = palette.map { |palette_entry|
col = COLORS[palette_entry]
MAIN.color((col & 0xFF0000) >> 16, (col & 0xFF00) >> 8, (col & 0xFF))
}
img.loadPixels
colors.each_index {|index|
x_offset = (index % 16) * 16
y_offset = (index / 16) * 16
(0...16).each {|y|
(0...16).each {|x|
img.pixels[((y_offset + y) * 256) + (x_offset + x)] = colors[index]
}
}
}
img.updatePixels
end
end
# Class to enable the pattern table viewer functionality
class PatternTablesViewer
include Palette
include Constants
def initialize(nes)
super()
@nes = nes
end
def repaint(img)
ppu = @nes.ppu
mmc = ppu.mmc
bit_masks = [0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1]
byte_1_bit_shift = [7, 6, 5, 4, 3, 2, 1, 0]
byte_2_bit_shift = [6, 5, 4, 3, 2, 1, 0, -1]
pattern_table0_palette_buffer = Array.new(128, nil)
pattern_table0_palette_buffer.each_index {|index|
pattern_table0_palette_buffer[index] = Array.new(128, 0)
}
pattern_table1_palette_buffer = Array.new(128, nil)
pattern_table1_palette_buffer.each_index {|index|
pattern_table1_palette_buffer[index] = Array.new(128, 0)
}
# Fill in onscreen buffers from the pattern tables
for pattern_table_index in (PATTERN_TABLE_0_LO..PATTERN_TABLE_0_HI)
if pattern_table_index % 16 < 8
tile = (pattern_table_index / 16).floor # Tiles are 16 bytes a piece
tile_row = (tile / 16).floor # 16 tiles in a scanline
scanline_index = (tile_row * 8) + (pattern_table_index % 8)
pixel_index = (tile - (tile_row * 16)) * 8
pattern_table_byte = mmc.read_ppu_mem(pattern_table_index)
pattern_table_byte2 = mmc.read_ppu_mem(pattern_table_index + 8)
pixels = combine_pattern_table_bytes(pattern_table_byte, pattern_table_byte2)
(0..7).each {|index|
pattern_table0_palette_buffer[scanline_index][pixel_index + index] = pixels[index]
}
end
end
for pattern_table_index in (PATTERN_TABLE_1_LO..PATTERN_TABLE_1_HI)
if pattern_table_index % 16 < 8
tile = ((pattern_table_index - PATTERN_TABLE_1_LO) / 16).floor # Tiles are 16 bytes a piece
tile_row = (tile / 16).floor # 16 tiles in a scanline
scanline_index = (tile_row * 8) + (pattern_table_index % 8)
pixel_index = (tile - (tile_row * 16)) * 8
pattern_table_byte = mmc.read_ppu_mem(pattern_table_index)
pattern_table_byte2 = mmc.read_ppu_mem(pattern_table_index + 8)
pixels = combine_pattern_table_bytes(pattern_table_byte, pattern_table_byte2)
(0..7).each {|index|
pattern_table1_palette_buffer[scanline_index][pixel_index + index] = pixels[index]
}
end
end
img.loadPixels
pattern_table0_palette_buffer.each_index { |scanline|
line = pattern_table0_palette_buffer[scanline]
line.each_index { |pixel|
# At the moment, not indexing into the image palette, since it doesn't seem to be initializing correctly
dot = COLORS[line[pixel]]
img.pixels[(scanline * 128) + pixel] = MAIN.color((dot & 0xFF0000) >> 16, (dot & 0xFF00) >> 8, (dot & 0xFF))
}
}
pattern_table1_palette_buffer.each_index { |scanline|
line = pattern_table1_palette_buffer[scanline]
line.each_index { |pixel|
# At the moment, not indexing into the image palette, since it doesn't seem to be initializing correctly
dot = COLORS[line[pixel]]
img.pixels[((scanline + 128) * 128) + pixel] = MAIN.color((dot & 0xFF0000) >> 16, (dot & 0xFF00) >> 8, (dot & 0xFF))
}
}
img.updatePixels
end
def combine_pattern_table_bytes(byte0, byte1)
result = Array.new(8, 0)
result[0] = ((byte0 & 0x80) >> 7) | ((byte1 & 0x80) >> 6)
result[1] = ((byte0 & 0x40) >> 6) | ((byte1 & 0x40) >> 5)
result[2] = ((byte0 & 0x20) >> 5) | ((byte1 & 0x20) >> 4)
result[3] = ((byte0 & 0x10) >> 4) | ((byte1 & 0x10) >> 3)
result[4] = ((byte0 & 0x08) >> 3) | ((byte1 & 0x08) >> 2)
result[5] = ((byte0 & 0x04) >> 2) | ((byte1 & 0x04) >> 1)
result[6] = ((byte0 & 0x02) >> 1) | (byte1 & 0x02)
result[7] = (byte0 & 0x01) | ((byte1 & 0x01) << 1)
return result
end
end