forked from docmarionum1/jsAtari
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMU.js
More file actions
211 lines (167 loc) · 5.08 KB
/
MMU.js
File metadata and controls
211 lines (167 loc) · 5.08 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
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
/*
jsAtari
Copyright (C) 2011 Jeremy Neiman
This file is part of jsAtari.
jsAtari is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
jsAtari is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with jsAtari. If not, see <http://www.gnu.org/licenses/>.
*/
mmu =
{
debug: false,
//Where the memory is stored
memory: new ArrayBuffer(0x10000),
/*views for each segment of memory*/
ram: null,
rom: null,
tia: null,
tia_write: null,
tia_read: null,
pia: null,
pia_write: null,
pia_read: null,
/*Memory Locations and Sizes*/
ram_start: 0x80, //128 bytes, located from 0x80 to 0xFF. Stack starts at 0xFF, down
ram_size: 0x80,
ram_end: 0xff,
rom_start: 0xf000, //ROM start location
rom_size: 0x1000, //4kb
rom_end: 0xffff,
rom_brk: 0xf000, //ROM break point
rom_reset: 0xf000, //ROM reset point
tia_write_start: 0x0,
tia_write_size: 0x2d,
tia_write_end: 0x2c,
tia_read_start: 0x30,
tia_read_size: 0xe,
tia_read_end: 0x3d,
pia_size: 0x18,
pia_read_start: 0x280,
pia_read_size: 0x5,
pia_read_end: 0x284,
pia_write_start: 0x294,
pia_write_size: 0x4,
pia_write_end: 0x297,
/*Read/Write*/
r: function(a)
{
//debug(a.toString(16));
if ((a%0x2000) < 0x1000)
{
a &= 0xfff;
//debug("hi");
}
else
a = 0xf000 + (a&0xfff);
//debug(a.toString(16));
if (a <= mmu.tia_read_end)
{
if(a >= mmu.tia_read_start)
a -= mmu.tia_read_start; //Hack for TIA read registers.
return mmu.tia_read[a];
}
else if (a >= mmu.ram_start && a <= mmu.ram_end)
return mmu.ram[a-mmu.ram_start];
else if (a >= mmu.pia_read_start && a <= mmu.pia_read_end)
{
return mmu.pia_read[a - mmu.pia_read_start];
}
else if (a >= mmu.rom_start && a <= mmu.rom_end)
return mmu.rom[a-mmu.rom_start];
else if (mmu.debug)
debug("Trying to read from memory address " + a + " - Denied");
},
w: function(a, v)
{
v = v&255;
if (a <= mmu.tia_write_end)
{
/*if (tia.filterStrobe(a))
return;
if (a == tia.reg.GRP0)
tia.p1.write = true;*/
//tia.write(a) = true;
tia.write = a;
mmu.tia_write[a] = v&255;
//if (a == 2)
// cpu.paused = true;
if (a == tia.reg.HMP0 || a == tia.reg.HMP1 || a == tia.reg.GRP0)
{
//debug("Writing " + v.toString(2) + " to " + a.toString(16));
}
}
else if (a >= mmu.ram_start && a <= mmu.ram_end)
mmu.ram[a-mmu.ram_start] = v;
else if (a >= mmu.pia_write_start && a <= mmu.pia_write_end)
{
pia.write = a - mmu.pia_read_start;
mmu.pia_write[a - mmu.pia_write_start] = v&255;
}
else if (mmu.debug)
debug("Trying to write to memory address " + a + " - Denied");
},
/*Stack*/
stackPush: function(v) {mmu.ram[cpu.r.s - mmu.ram_start] = v&255; cpu.r.s = (cpu.r.s - 1)&255;},
stackPushWord: function(v) {mmu.stackPush(v>>8); mmu.stackPush(v);},
stackPop: function() {var v = mmu.ram[cpu.r.s - mmu.ram_start + 1]; cpu.r.s = (cpu.r.s + 1)&255; return v;},
stackPopWord: function() {return (mmu.stackPop() + (mmu.stackPop() << 8));},
/*ROM*/
readByte: function()
{
var a = cpu.r.pc++;
if (!(a%0x2000) < 0x1000)
{
a = 0xf000 + (a&0xfff);
}
return mmu.rom[a - mmu.rom_start];
},
readWord: function()
{
var lowBit = mmu.rom[cpu.r.pc++ - mmu.rom_start];
var highBit = mmu.rom[cpu.r.pc++ - mmu.rom_start];
var total = lowBit + (highBit << 8);
//debug(lowBit + ", " + highBit + " = " + total);
return total;
},
loadRom: function(file)
{
var b = new BinFileReader(file);
var rom = b.readString(b.getFileSize(), 0);
mmu.loadRomString(rom);
},
loadRomString: function(rom)
{
for (var i = 0; i < rom.length; i++)
{
mmu.rom[i] = rom.charCodeAt(i);
}
var lowBit = mmu.rom[rom.length-4];
var highBit = mmu.rom[rom.length-3];
mmu.rom_reset = lowBit + (highBit << 8);
lowBit = mmu.rom[rom.length-2];
highBit = mmu.rom[rom.length-1];
mmu.rom_brk = lowBit + (highBit << 8);
//debug(lowBit + ", " + highBit + " = " + mmu.org);
},
__init__: function(debug)
{
mmu.ram = new Uint8Array(mmu.memory, mmu.ram_start, mmu.ram_size);
mmu.rom = new Uint8Array(mmu.memory, mmu.rom_start, mmu.rom_size);
mmu.tia = new Uint8Array(mmu.memory, mmu.tia_write_start, mmu.tia_read_end);
mmu.tia_write = new Uint8Array(mmu.memory, mmu.tia_write_start, mmu.tia_write_size);
mmu.tia_read = new Uint8Array(mmu.memory, mmu.tia_read_start, mmu.tia_read_size);
mmu.pia = new Uint8Array(mmu.memory, mmu.pia_read_start, mmu.pia_size);
mmu.pia_write = new Uint8Array(mmu.memory, mmu.pia_write_start, mmu.pia_write_size);
mmu.pia_read = new Uint8Array(mmu.memory, mmu.pia_read_start, mmu.pia_read_size);
mmu.debug = debug;
},
/*Reset*/
//reset: function() {mmu.ram = []; mmu.rom = "";}
};