forked from 6502/js6502
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6502.html
177 lines (161 loc) · 6.25 KB
/
6502.html
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
<!DOCTYPE html>
<!--
****************************************************************************
******************************************************************************
** **
** Copyright (c) 2012 by Andrea Griffini **
** **
** Permission is hereby granted, free of charge, to any person obtaining **
** a copy of this software and associated documentation files (the **
** "Software"), to deal in the Software without restriction, including **
** without limitation the rights to use, copy, modify, merge, publish, **
** distribute, sublicense, and/or sell copies of the Software, and to **
** permit persons to whom the Software is furnished to do so, subject to **
** the following conditions: **
** **
** The above copyright notice and this permission notice shall be **
** included in all copies or substantial portions of the Software. **
** **
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, **
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF **
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND **
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE **
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION **
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION **
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **
** **
******************************************************************************
****************************************************************************
-->
<html>
<head>
<title>6502 emulator</title>
</head>
<body>
<table border=0>
<tr><td>
Virtual graphic screen<br/>
<canvas id="canvas" style="width:512px; height:512px; border:solid 1px #000">
</canvas>
</td><td>
<select id="examples" onchange="loadexample()">
<option value="">Select a demo program</option>
<option value="demo1.asm">Graphic demo</option>
<option value="demo2.asm">Self-modifying code</option>
<option value="demo0.asm">Special locations only</option>
</select>
<textarea id="source" style="height:508px; width:512px;">...put your code...</textarea>
<input type=button value="Run" onclick="doit()">
</td></tr></table>
<script src="6502.js"></script>
<script>
function http(verb, url, data, onSuccess, onFail)
{
var req = new XMLHttpRequest();
if (onSuccess) {
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
onSuccess(req.responseText, url, req);
} else {
if (onFail)
onFail(url, req.status);
else
throw new String("Ajax request error (url=" +
url +
", status=" +
req.status + ")");
}
}
}
}
req.open(verb, url, !!onSuccess);
req.send(data);
return onSuccess ? req : req.responseText;
}
function loadexample()
{
var example_name = document.getElementById("examples").value;
http("GET", example_name, "", function(x){
document.getElementById("source").value = x;
});
}
// Virtual screen interface /////////////////////////////////////////////////////
var canvas = document.getElementById("canvas");
canvas.width = canvas.height = 256;
var ctx = canvas.getContext("2d");
var data = ctx.getImageData(0, 0, 256, 256);
var bytes = data.data;
var current_palette = [];
var current_palette_index = 0;
var current_palette_comp = 0;
var current_address = 0;
(function(){
for (var r=0; r<6; r++)
for (var g=0; g<6; g++)
for (var b=0; b<6; b++)
current_palette.push([r*51, g*51, b*51]);
for (var g=0; g<40; g++)
current_palette.push([Math.floor(g*255/39+0.5),
Math.floor(g*255/39+0.5),
Math.floor(g*255/39+0.5)]);
})();
function set_palette(x) { current_palette_index = x; current_palette_comp = 0; }
function write_color(x)
{
current_palette[current_palette_index][current_palette_comp] = x;
if (++current_palette_comp == 3)
{
current_palette_comp = 0;
current_palette_index = (current_palette_index + 1) & 255;
}
}
function set_row(x) { current_address = x*256*4 + (current_address & (255*4)); }
function set_col(x) { current_address = x*4 + (current_address & (255*256*4)); }
function write_pixel(x)
{
var c = current_palette[x];
bytes[current_address] = c[0];
bytes[current_address+1] = c[1];
bytes[current_address+2] = c[2];
current_address = (current_address + 4) & (256*256*4-1);
}
(function(){
for (var i=0; i<256*256; i++)
bytes[i*4+3] = 255;
ctx.putImageData(data, 0, 0);
})();
// Random numbers and time tracking
var ctime = 0;
function random() { return (Math.random() * 256) & 255; }
function clock0() { ctime = (new Date).getTime(); return ctime&255; }
function clock1() { return (ctime >> 8) & 255; }
function clock2() { return (ctime >> 16) & 255; }
function clock3() { return (ctime >> 24) & 255; }
// Note: special read/write only works for LDA/STA absolute
special_write[0xFF00] = set_palette;
special_write[0xFF01] = write_color;
special_write[0xFF02] = set_row;
special_write[0xFF03] = set_col;
special_write[0xFF04] = write_pixel;
special_read[0xFF80] = random;
special_read[0xFF81] = clock0;
special_read[0xFF82] = clock1;
special_read[0xFF83] = clock2;
special_read[0xFF84] = clock3;
function doit()
{
try
{
assemble(document.getElementById("source").value);
}
catch(err)
{
alert(err);
return;
}
setTimeout(run, 0);
}
</script>
</body>
</html>