-
Notifications
You must be signed in to change notification settings - Fork 0
/
nes.mg
168 lines (129 loc) · 4.65 KB
/
nes.mg
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
import mem "mem";
import rom "rom";
import bus "bus";
import cpu "cpu";
import ppu "ppu";
import fmt "fmt";
import joypad "joypad";
@main()
fn main() {
bus::init();
}
let remaining_elapsed_nanosecond: i64 = 0;
let cycle_rate: i64 = 1_789_773; // cycles per second
let cycle_period: i64 = 1_000_000_000 / cycle_rate;
@wasm_export("tick")
fn tick(elapsed_ns: i64) {
remaining_elapsed_nanosecond = remaining_elapsed_nanosecond + elapsed_ns;
if remaining_elapsed_nanosecond > 16_000_000 {
remaining_elapsed_nanosecond = 16_000_000;
}
let cpu_cycle = remaining_elapsed_nanosecond / cycle_period;
while cpu_cycle > 0 {
let cycle = cpu::tick(bus::the_cpu) as i64;
ppu::tick(bus::the_ppu, cycle*3);
cpu_cycle = cpu_cycle - cycle;
}
remaining_elapsed_nanosecond = cpu_cycle;
ppu::render(bus::the_ppu);
}
let rom_buffer: [*]u8 = mem::alloc_array::<u8>(0x100000);
@wasm_export("getRom")
fn get_rom(): [*]u8 {
return rom_buffer;
}
struct LoadRomResult {
valid: bool,
error: [*]u8,
}
@wasm_export("loadRom")
fn load_rom(): LoadRomResult {
let result = rom::load(rom_buffer);
if !result.valid {
return LoadRomResult{valid: false, error: result.error};
}
bus::the_rom.* = result;
ppu::load_rom(bus::the_ppu, bus::the_rom);
return LoadRomResult{valid: true, error: 0 as [*]u8};
}
@wasm_export("getRam")
fn get_ram(): [*]u8 {
return bus::ram as [*]u8;
}
@wasm_export("reset")
fn reset() {
bus::reset();
rom::reset(bus::the_rom);
cpu::reset(bus::the_cpu);
ppu::reset(bus::the_ppu);
joypad::reset(bus::joypad_1);
joypad::reset(bus::joypad_2);
}
@wasm_export("debugCPU")
fn debug_cpu(): cpu::CPU {
return bus::the_cpu.*;
}
@wasm_export("getDebugTileFramebufer")
fn get_debug_tile_framebuffer(): ppu::Image {
return ppu::get_debug_tile_framebuffer(bus::the_ppu);
}
@wasm_export("getDebugPaletteImage")
fn get_debug_palette_framebuffer(): ppu::DebugPalette {
return ppu::get_debug_palette_framebuffer(bus::the_ppu);
}
@wasm_export("setDebugPaletteId")
fn set_debug_palette_id(id: u8) {
bus::the_ppu.debug.debug_palette_id.* = id;
}
@wasm_export("getScreenFramebuffer")
fn get_screen_framebuffer(): ppu::Image {
return ppu::get_screen_framebuffer(bus::the_ppu);
}
@wasm_export("getNametable1Framebuffer")
fn get_nametable_1_framebuffer(): ppu::Image {
return ppu::get_nametable_1_framebuffer(bus::the_ppu);
}
@wasm_export("getNametable2Framebuffer")
fn get_nametable_2_framebuffer(): ppu::Image {
return ppu::get_nametable_2_framebuffer(bus::the_ppu);
}
@wasm_export("getNametable3Framebuffer")
fn get_nametable_3_framebuffer(): ppu::Image {
return ppu::get_nametable_3_framebuffer(bus::the_ppu);
}
@wasm_export("getNametable4Framebuffer")
fn get_nametable_4_framebuffer(): ppu::Image {
return ppu::get_nametable_4_framebuffer(bus::the_ppu);
}
@wasm_export("keydownJoypad1A")
fn keydown_joypad1_a() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_A); }
@wasm_export("keydownJoypad1B")
fn keydown_joypad1_b() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_B);}
@wasm_export("keydownJoypad1Select")
fn keydown_joypad1_select() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_SELECT);}
@wasm_export("keydownJoypad1Start")
fn keydown_joypad1_start() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_START);}
@wasm_export("keydownJoypad1Up")
fn keydown_joypad1_up() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_UP);}
@wasm_export("keydownJoypad1Down")
fn keydown_joypad1_down() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_DOWN); }
@wasm_export("keydownJoypad1Left")
fn keydown_joypad1_left() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_LEFT);}
@wasm_export("keydownJoypad1Right")
fn keydown_joypad1_right() { joypad::press(bus::joypad_1, joypad::JOYPAD_BUTTON_RIGHT);}
@wasm_export("keyupJoypad1A")
fn keyup_joypad1_a() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_A); }
@wasm_export("keyupJoypad1B")
fn keyup_joypad1_b() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_B);}
@wasm_export("keyupJoypad1Select")
fn keyup_joypad1_select() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_SELECT);}
@wasm_export("keyupJoypad1Start")
fn keyup_joypad1_start() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_START);}
@wasm_export("keyupJoypad1Up")
fn keyup_joypad1_up() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_UP);}
@wasm_export("keyupJoypad1Down")
fn keyup_joypad1_down() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_DOWN);}
@wasm_export("keyupJoypad1Left")
fn keyup_joypad1_left() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_LEFT);}
@wasm_export("keyupJoypad1Right")
fn keyup_joypad1_right() { joypad::unpress(bus::joypad_1, joypad::JOYPAD_BUTTON_RIGHT);}