-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu.js
275 lines (227 loc) · 8.5 KB
/
gpu.js
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
const {bitarray8r, bit_at, to_signed_int8, bitarray8_to_number, bitarray8} = require('./utils.js');
let array8 = [0,1,2,3,4,5,6,7];
class GPUStats{
constructor(){
this.stat = {
bit0:0, bit1:0,
coin_flag:0,
hblank_int:0,
vblank_int:0,
oam_int:0,
coin_int:0,
};
this.keys = Object.keys(this.stat);
}
set_byte(v){
let cm = this.get_mode();
let ba = bitarray8(v);
for(let i = 0; i < 8; i++)
this.stat[this.keys[i]] = ba[i];
this.set_mode(cm);
}
get_byte(){
return bitarray8_to_number([...this.keys.map(i => this.stat[i]), 0]);
}
set_mode(v){
this.stat.bit0 = v & 0x01;
this.stat.bit1 = v >> 1;
}
get_mode(){
return (this.stat.bit1 << 1) + this.stat.bit0;
}
}
class GPU{
constructor(){
this.cpu = null;
this.screen_out = Array(160 * 144).fill(0);
this.vram = Array(0x2000).fill(0);
this.oam = Array(0xa0).fill(0);
this.lcdc = 0;
this.stat = new GPUStats();
this.scy = 0; this.scx = 0; //background scroll
this.ly = 0; // current drawing line
this.lyc = 0; //line compare
this.wy = 0; this.wx = 0; //window position
this.bg_pal = 0;
this.obj_pal0 = 0;
this.obj_pal1 = 0;
this.tile_cache = Array(384).fill(0); //set all cache dirty.
this.clock = 0;
this.frames = 0;
// this.mode_cycles = [207, 4560, 83, 175]; // HBlank, VBlank, OAM Read, OAM and VRAM Read
}
set_lcd_enable(e){let code = e << 7; this.lcdc = (this.lcdc & ~code) | code;}
lcd_enabled(){return this.lcdc & 0x80;}
set_window_tilemap(v){ let code = v << 6; this.lcdc = (this.lcdc & ~code) | code; throw "GPU: Window not implemented.";}
window_tilemap(){ return this.lcdc & 0x40;}
set_window_enable(e){ let code = e << 5; this.lcdc = (this.lcdc & ~code) | code; throw "GPU: Window not implemented.";}
window_enabled(){ return this.lcdc & 0x20;}
set_tile_data_select(v){ let code = v << 4; this.lcdc = (this.lcdc & ~code) | code;}
tile_data_select(){return this.lcdc & 0x10;}
set_background_tilemap(v){let code = v << 3; this.lcdc = (this.lcdc & ~code) | code;}
background_tilemap(){return this.lcdc * 0x8;}
set_sprite_size(v){let code = v << 2; this.lcdc = (this.lcdc & ~code) | code;}
sprite_size(){return this.lcdc & 0x4;}
set_sprite_enable(e){let code = e << 1; this.lcdc = (this.lcdc & ~code) | code; throw "GPU: Sprite not implemented."}
sprite_enabled(){return this.lcdc & 0x2;}
set_background_enabled(v){let code = v << 0; this.lcdc = (this.lcdc & ~code) | code;}
background_enabled(){return this.lcdc & 0x1;}
mode(){ return this.stat.get_mode();}
set_mode(m){ this.stat.set_mode(m);}
tick(cycles){
if(!this.lcd_enabled()){
// this.reset();
return;
}
this.clock += cycles;
if(this.clock > 456){
this.clock = this.clock - 456;
this.ly++;
}
this.tick_line();
}
tick_line(){
let should_renderer = false;
//A cycle is finished
if(this.ly > 153) this.ly = 0;
//During VBlank
if(this.ly > 143 && this.mode() !== 1){
this.set_mode(1);
this.cpu.request_vblank_interrupt();
this.frames++;
this.generate_tile_cache();
// if(this.frames % 20 === 0) this.print_screen();
return;
}
if(this.ly > 143) return; //If in VBlank, there is nothing to do.
//Line number is less than 144, normal drawing..
//During OAM Read
if(this.clock <= 80 && this.mode() !== 2){
this.set_mode(2);
//TODO: OAM STAT Interrupt..
}
if(this.clock > 80 && this.clock <= 252 && this.mode() !== 3 && this.mode() !== 1){
this.set_mode(3);
//TODO: All Interrupt
//really ?
}
//During HBlank
if(this.clock > 252 && this.mode() !== 0 && this.mode() !== 1){
this.set_mode(0);
//TODO: HBlank Interrupt
should_renderer = true;
}
if(!should_renderer) return;
if(this.background_enabled()) this.draw_background(this.ly);
//TODO: draw this line
}
draw_background(l){
let tm_offset = this.background_tilemap()? 0x9800 : 0x9c00;
let tile_y = ((l + this.scy) & 0xff) >> 3; //warp and divide by 8.
let y = (l + this.scy) & 0x7;
for(let x = 0; x < 160; x++){
let tile_x = ((this.scx + x) & 0xff) >> 3;
let t_addr = this.read_byte(tm_offset + tile_y * 32 + tile_x);
let tile = this.tile_at(t_addr);
if(tile === 0) continue;
this.screen_out[160 * l + x] = this.color_for(this.bg_pal, tile[y][(x + this.scx) & 7]);
}
}
tile_at(addr){
let offset = this.tile_data_select()? 0 : 128;
addr = this.tile_data_select()? addr : to_signed_int8(addr);
return this.tile_cache[addr];
}
generate_tile_cache(){
let offset = 0x8000;
for(let t = 0; t < 384; t++){
if(this.tile_cache[t] !== 0) continue;
let d = [...Array(16).keys()].map(i => bitarray8r(this.read_byte(offset + i + (t * 16))));
this.tile_cache[t] = array8.map(i =>
array8.map(bit =>
d[i * 2][bit] + d[i * 2 + 1][bit] * 2
)
);
}
}
debug_draw_tile1(){
let sc = [];
for(let i = 0; i < 256; i++){
sc.push(this.tile_at((i * 16)));
}
return sc;
}
color_for(palette, v){
let p = bitarray8r(palette);
let b = 3 - v;
return p[b*2] + (p[b*2+1] << 1);
}
read_byte(addr){
switch (addr & 0xf000) {
case 0x8000:
case 0x9000:
return this.vram[addr & 0x1fff];
case 0xf000:
if(addr >= 0xfe00 && addr < 0xfea0)
return this.oam[addr & 0xff];
switch(addr){
case 0xff40: return this.lcdc;
case 0xff41: return this.stat.get_byte();
case 0xff42: return this.scy;
case 0xff43: return this.scx;
case 0xff44: return this.ly;
case 0xff45: return this.lyc;
case 0xff4a: return this.wy;
case 0xff4b: return this.wx;
case 0xff47: return this.bg_pal;
case 0xff48: return this.obj_pal0;
case 0xff49: return this.obj_pal1;
default: throw `GPU: Unknown address ${addr.toString(16)}`;
}
default:
throw `GPU: Unknown address ${addr.toString(16)}`;
}
}
write_byte(addr, v){
switch (addr & 0xf000) {
case 0x8000:
case 0x9000:
this.tile_cache = Array(384).fill(0); //TODO: determine dirty.
this.vram[addr & 0x1fff] = v;
break;
case 0xf000:
if(addr >= 0xfe00 && addr < 0xfea0){
this.oam[addr & 0xff] = v;
break;
}
switch (addr) {
case 0xff40: this.lcdc = v; break;
case 0xff41: this.stat.set_byte(v);//return this.stat;
case 0xff42: this.scy = v; break;
case 0xff43: this.scx = v; break;
case 0xff44: throw "GPU: Don't fuck with line counter.";
case 0xff45: throw "GPU: LYC not implemented.";
case 0xff4a: this.wy = v; break;
case 0xff4b: this.wx = v; break;
case 0xff47: this.bg_pal = v; break;
case 0xff48: this.obj_pal0 = v; break;
case 0xff49: this.obj_pal1 = v; break;
default: console.log(`GPU: Writing to unknown address ${addr.toString(16)}`);
}
break;
default:
console.log(`GPU: Writing to unknown address ${addr.toString(16)}`);
}
}
print_screen(){
let s = "";
for(let y = 0; y < 144; y++){
for(let x = 0; x < 160; x++){
s += this.screen_out[y * 160 + x].toString();
}
s += "\n";
}
console.log(s);
}
}
module.exports = GPU;