-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartridge.js
106 lines (90 loc) · 2.71 KB
/
cartridge.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
function Cartridge (buffer) {
window.cart = this
this.nMapperId = 0
this.nPRGBanks = 0
this.nCHRBanks = 0
this.vPRGMemory = []
this.vCHRMemory = []
this.bImageValid = false
this.trainerOffest = 0
this.header = {
headerString: '',
numRomBanks: 0,
numVRomBanks: 0,
info: [],
numPrgRamBanks: 0,
info2: '',
reserved: [],
sizeOfHeader: 16,
prgRomSize: 16384,
chrRomSize: 8192
}
this.data = new Uint8Array(buffer)
console.log('CARTRIDGE: ', this.data)
this.header.headerString = new TextDecoder("ascii").decode(this.data.slice(0, 4))
this.header.numRomBanks = this.data.slice(4, 5)[0]
this.header.numVRomBanks = this.data.slice(5, 6)[0]
this.header.info.push(...this.data.slice(6, 8))
this.header.numPrgRamBanks = this.data.slice(8, 9)[0]
this.header.info2 = this.data.slice(9, 10)[0]
this.header.reserved.push(...this.data.slice(10, 16))
console.log('HEADER: ', this.header)
if (this.header.info[0] & 0x04) this.trainerOffest = 512
this.nMapperId = ((this.header.info[1] >> 4) << 4) | (this.header.info[0] >> 4)
this.mirror = (this.header.info[0] & 0x01) ? 'VERTICAL' : 'HORIZONTAL'
this.nFileType = 1
if (this.nFileType == 0) {}
if (this.nFileType == 1) {
this.nPRGBanks = this.header.numRomBanks
const prgOffest = (this.header.prgRomSize * this.nPRGBanks) + this.trainerOffest + 16
this.vPRGMemory = this.data.slice(16, prgOffest)
this.nCHRBanks = this.header.numVRomBanks
const chrOffset = prgOffest + (this.header.chrRomSize * (this.nCHRBanks || 1))
this.vCHRMemory = this.data.slice(prgOffest, chrOffset)
}
if (this.nFileType == 2) {}
this.bImageValid = true
switch (this.nMapperId) {
case 0:
this.pMapper = new Mapper000(this.nPRGBanks, this.nCHRBanks)
break
}
console.log('vPRGMemory: ', this.vPRGMemory)
console.log('vCHRMemory: ', this.vCHRMemory)
this.imageValid = () => {
return this.bImageValid
}
this.cpuRead = (addr) => {
const read = this.pMapper.cpuMapRead(addr)
if (read.output) {
return { data: this.vPRGMemory[read.mappedAddr], output: true }
}
else return { output: false }
}
this.cpuWrite = (addr, data) => {
const read = this.pMapper.cpuMapWrite(addr)
if (read.output) {
this.vPRGMemory[read.mappedAddr] = data
return true
}
else return false
}
this.ppuRead = (addr) => {
const read = this.pMapper.ppuMapRead(addr)
if (read.output) {
return { data: this.vCHRMemory[read.mappedAddr], output: true }
}
else return { output: false }
}
this.ppuWrite = (addr, data) => {
const read = this.pMapper.ppuMapWrite(addr)
if (read.output) {
this.vCHRMemory[read.mappedAddr] = data
return true
}
else return false
}
this.reset = () => {
if (this.pMapper) this.pMapper.reset()
}
}