-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulator.html
466 lines (422 loc) · 14.9 KB
/
emulator.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<!--
Main Interface page for Arduino Hackvision emulator
-->
<html>
<head>
<title>Arduino Hackvision Emulator</title>
<!-- Some CSS -->
<style>
body
{
//background-color:#ff9900;
background-color:#0099ff;
}
#tv_canvas
{
visibility: hidden;
}
textarea
{
background-color:#000000;
color:#00ff00;
font-size:14px
}
</style>
</head>
<body onload="initialize()">
<script src="atmega328.js"></script>
<script src="television.js"></script>
<script src="hex_file_load.js"></script>
<script type="text/javascript">
var inter = 0; //Interval for callback for processor next state
var tv_iter = 0; //Interval for updating TV with contents of framebuffer
var sp_label_object = ""; //Reference to HTML object holding stack pointer in debugger
var sreg_label_object = ""; //Reference to HTML object displaying status register in debugger
var timer_1_label_object = ""; //Reference to HTML object displaying TCNT1 in debugger
var tccr1a_label_object = ""; //Reference to HTML object displaying TCCR1A in debugger
var tccr1b_label_object = ""; //Reference to HTML object displaying TCCR1B in debugger
var tccr1c_label_object = ""; //Reference to HTML object displaying TCCR1C in debugger
var reg_label_objects = new Array(); //Array of references to HTML objects displaying the contents of the registers in debugger
var pc_label_object = ""; //Reference to HTML object displaying the position of the program counter in debugger
var current_instr_object = ""; //Reference to HTML object displaying current instruction being executed in debugger
var current_op_object = ""; //Reference to HTML object displaying current opcode being executed
var total_cycles = 0; //Total number of instructions which have been executed
var total_cpu_ticks = 0; //Total number of CPU cycles
//Hack variables --- Parts of the program where the emulator takes control of the normal program execution flow
var hack_tvout_delay = 0;
var hack_tvout_delay_frame = 0;
var hack_analog_read = 0;
function update_debugger()
{
pc_label_object.value = PC.toString(16); //Update program counter in debugger
name_current_instruction(); //Get the name of the current instruction
current_instr_object.value = current_instruction; //Display the name of the current instruction in debugger
sreg_label_object.value = io_memory[63].toString(2); //Display the Status Register in debugger
current_op_object.value = rom[PC].toString(16); //Display the current opcode in debugger
tccr1a_label_object.value = pad_8bit(ext_io_memory[32].toString(2)); //Display TCCR1A in debugger
tccr1b_label_object.value = pad_8bit(ext_io_memory[33].toString(2)); //Display TCCR1B in debugger
tccr1c_label_object.value = pad_8bit(ext_io_memory[34].toString(2)); //Display TCCR1C in debugger
return;
} // end update_debugger()
//Useful for debugging - returns the value of a floating point number stored at sram[p]...sram[p+3]
//https://en.wikipedia.org/wiki/Single_precision_floating-point_format
function dump_float(p)
{
var data = (sram[p+3] << 24) | (sram[p+2] << 16) | (sram[p+1] << 8) | (sram[p]);
var sign = (data >> 31) & 1;
var fraction = data & 8388607;
var exponent = (data >> 23) & 255;
var number = 0;
for (var ix=1; ix<23; ix++)
{
number = number + ((fraction >> (23 - ix)) & 1) * Math.pow(2, -1*ix);
}
number = number + 1;
number = number * Math.pow(2,exponent - 127);
number = number * Math.pow(-1, sign);
return number;
}
//Same as dump_float but for register and not sram
function dump_register_float(p)
{
var data = (registers[p+3] << 24) | (registers[p+2] << 16) | (registers[p+1] << 8) | (registers[p]);
var sign = (data >> 31) & 1;
var fraction = data & 8388607;
var exponent = (data >> 23) & 255;
var number = 0;
for (var ix=1; ix<23; ix++)
{
number = number + ((fraction >> (23 - ix)) & 1) * Math.pow(2, -1*ix);
}
number = number + 1;
number = number * Math.pow(2,exponent - 127);
number = number * Math.pow(-1, sign);
return number;
}
//Useful for debugging - places a floating point number (float_num) into registers[reg_index]...registers[reg_index+3]
function inject_register_float(reg_index, float_num)
{
var sign = 0;
if (float_num < 0)
{
sign = 1;
}
var exponent = 0;
var absolute_number = Math.abs(float_num);
var fraction = 0;
var normalized_number = absolute_number;
//Normalize the absolute_number so that it is less than one. I.E. in the form a*(2^-1) + b*(2^-2) + c*(2^-3) + ....
//If number is greater than, or equal to, one, repeatedly divide by 2
if (normalized_number >= 1)
{
while (normalized_number >= 1)
{
normalized_number = normalized_number / 2;
exponent = exponent + 1;
}
}
//Else, number is less than one, repeatedly multiply by 2
else
{
while (normalized_number*2 < 1)
{
normalized_number = normalized_number * 2;
exponent = exponent - 1;
}
}
//RECALCULATE normalized_number so that (1 + normalized_number) * 2^exponent = abs(float_num)
//2^exponent + new_normalized_number*2^exponent = abs(float_num)
//new_normalized_number = (abs(float_num) - 2^exponent) / 2^exponent
//new_normalized_number = (abs(float_num) / 2^exponent) - 1
exponent = exponent - 1;
normalized_number = (Math.abs(float_num) / Math.pow(2,exponent));
//Build the fractional part of the number
var temp_normalized_number = normalized_number - 1;
var bit_value = 4194304;
for (var ex = -1; ex > -22; ex--)
{
if ((temp_normalized_number / Math.pow(2,ex)) >= 1)
{
temp_normalized_number = temp_normalized_number - Math.pow(2,ex);
fraction = fraction + bit_value;
//console.log("Is divisable by 2^" + ex);
}
bit_value = bit_value / 2;
}
exponent = exponent + 127;
//console.log("Exponent: " + exponent);
//Store in processor registers
registers[reg_index] = fraction & 255;
registers[reg_index + 1] = (fraction >> 8) & 255;
registers[reg_index + 2] = (fraction >> 16) & 127;
registers[reg_index + 2] = registers[reg_index + 2] | (exponent & 1) << 7;
registers[reg_index + 3] = (exponent >> 1) & 127;
registers[reg_index + 3] = registers[reg_index + 3] | (sign & 1) << 7;
}
//Useful for debugging - dumps 32 bits from sram[p] to sram[p+3] as binary in form xxxx xxxx xxxx xxxx. Useful for debugging floating point numbers
function bin_dump_float(p)
{
//var data = (sram[p+3] << 24) | (sram[p+2] << 16) | (sram[p+1] << 8) | (sram[p]);
var data_str = pad_8bit(sram[p+3].toString(2)) + " " + pad_8bit(sram[p+2].toString(2)) + " " + pad_8bit(sram[p+1].toString(2)) + " " + pad_8bit(sram[p].toString(2));
return data_str;
}
//Same as bin_dump_float but for registers, not sram.
function bin_dump_register_float(p)
{
//var data = (sram[p+3] << 24) | (sram[p+2] << 16) | (sram[p+1] << 8) | (sram[p]);
var data_str = pad_8bit(registers[p+3].toString(2)) + " " + pad_8bit(registers[p+2].toString(2)) + " " + pad_8bit(registers[p+1].toString(2)) + " " + pad_8bit(registers[p].toString(2));
return data_str;
}
//Advance the debugger
function advance()
{
run_instruction();
run_eeprom();
clear_cycles();
update_debugger();
update_regs();
return;
} // end advance()
//Normally advance the program
function advance_no_debug()
{
for (var i = 0; i <=400; i++) //Forces more instructions to be executed in a given time. Mileage may vary from browser to browser. This was tested on Iceweasel 31.6.0
{
total_cycles++;
run_instruction();
// HACK to implement TVout::Delay()
if (PC == hack_tvout_delay)
{
// ATMEGA 328 return
//ret();
instruction_table[57]();
}
// HACK to implement TVout::delay_frame(unsigned int x)
if (PC == hack_tvout_delay_frame)
{
//ret();
instruction_table[57]();
}
// HACK to implement analog read
if (PC == hack_analog_read)
{
registers[24] = registers[24] & 191;
}
total_cpu_ticks += cycles;
}
} // end advance_no_debug()
//Load the HEX file into the virtual atmega328 ROM.
function load_program()
{
load_hex();
//Set up program constants
set_permanent_fb_addr();
hack_tvout_delay = document.getElementById("tvout_delay_address").value;
hack_tvout_delay_frame = document.getElementById("tvout_delay_frame_address").value;
hack_analog_read = document.getElementById("analog_read_address").value;
//Set up TV, this will automatically get x_resolution and y_resolution constants
tv_init();
//Other hooks to be called when program is loaded can be placed here
}
//Start automatic callbacks which make processor execute instructions and TV display framebuffer
function run_hackvision()
{
alert("Hackvision is running");
inter = setInterval("advance_no_debug()",0);
tv_iter = setInterval("display_permanent_frame_buffer()", 1);
}
//Stop automatic callbacks which make processor execute instructions and TV display framebuffer
function pause_hackvision()
{
alert("Hackvision is paused");
window.clearInterval(inter);
window.clearInterval(tv_iter);
}
</script>
<h1>Arduino Hackvision Emulator</h1>
<!-- Television is here. This is invisible. Data is written directly to it. -->
<canvas id="tv_canvas" width="120" height="96"></canvas>
<!-- Larger TV HERE - This is the canvas which acts as the visible TV -->
<canvas id="big_screen" width="480" height="384"></canvas>
<!-- BEGIN DEBUGGER -->
<table border="1">
<tr>
<td>
<h2>Debugger</h2>
</br>
</br>
Program Counter:<input readonly id="pc_label"></input>
</br>
Current 16-bit Opcode:<input readonly id="current_op"></input>
</br>
Current Instruction:<input readonly id="current_instr"</input>
</br>
SREG:<input readonly id="sreg_label"></input>
</br>
</br>
</br>
Register 0:<input readonly id="reg0"></input>
</br>
Register 1:<input readonly id="reg1"></input>
</br>
Register 2:<input readonly id="reg2"></input>
</br>
Register 3:<input readonly id="reg3"></input>
</br>
Register 4:<input readonly id="reg4"></input>
</br>
Register 5:<input readonly id="reg5"></input>
</br>
Register 6:<input readonly id="reg6"></input>
</br>
Register 7:<input readonly id="reg7"></input>
</br>
Register 8:<input readonly id="reg8"></input>
</br>
Register 9:<input readonly id="reg9"></input>
</br>
Register 10:<input readonly id="reg10"></input>
</br>
Register 11:<input readonly id="reg11"></input>
</br>
Register 12:<input readonly id="reg12"></input>
</br>
Register 13:<input readonly id="reg13"></input>
</br>
Register 14:<input readonly id="reg14"></input>
</br>
Register 15:<input readonly id="reg15"></input>
</br>
Register 16:<input readonly id="reg16"></input>
</br>
Register 17:<input readonly id="reg17"></input>
</br>
Register 18:<input readonly id="reg18"></input>
</br>
Register 19:<input readonly id="reg19"></input>
</br>
Register 20:<input readonly id="reg20"></input>
</br>
Register 21:<input readonly id="reg21"></input>
</br>
Register 22:<input readonly id="reg22"></input>
</br>
Register 23:<input readonly id="reg23"></input>
</br>
Register 24:<input readonly id="reg24"></input>
</br>
Register 25:<input readonly id="reg25"></input>
</br>
Register 26:<input readonly id="reg26"></input>
</br>
Register 27:<input readonly id="reg27"></input>
</br>
Register 28:<input readonly id="reg28"></input>
</br>
Register 29:<input readonly id="reg29"></input>
</br>
Register 30:<input readonly id="reg30"></input>
</br>
Register 31:<input readonly id="reg31"></input>
</br>
</br>
TCNT1:<input readonly id="timer_1_label"></input>
</br>
TCCR1A<input readonly id="tccr1a_label"></input>
</br>
TCCR1B<input readonly id="tccr1b_label"></input>
</br>
TCCR1C<input readonly id="tccr1c_label"></input>
</br>
</br>
Stack Pointer:<input readonly id="sp_label"></input>
</br>
</br>
Return address:<input readonly id="return_label"></input>
</br>
Last Call:<input readonly id="last_call_label"></input>
</br>
</br>
</td>
<td>
<input type="button" value="Step" onclick="advance()"></input>
<input type="button" value="Update Registers" onclick="update_regs()"></input>
</br>
<br>
Jump to:<input id="jmp_address_label"></input>
</br>
<input type="button" value="Jump!" onclick="debug_jump()"></input>
</br>
</br>
</br>
Frame Buffer Address (in decimal):<input id="fb_addr_label"></input>
</br>
<input type="button" value="Display" onclick="display_frame_buffer()"></input>
</br>
</br>
<input type="button" value="Run" onclick="run_hackvision()"></input>
<input type="button" value="Pause" onclick="pause_hackvision()"></input>
</td>
<td>
<b>Paste HEX file here</b>
</br>
<textarea id="hex_txt" rows="20" cols="40">
</textarea>
</br>
</br>
<b>Program Constants</b>
</br>
Frame Buffer Address(in decimal):
<input id="const_fb_addr"></input>
</br>
</br>
TVout::Delay() Address(in decimal):
<input id="tvout_delay_address"></input>
</br>
</br>
TVout::delay_frame(unsigned int x) Address (in decimal):
<input id="tvout_delay_frame_address"></input>
</br>
</br>
analogRead Address (in decimal):
<input id="analog_read_address"></input>
</br>
</br>
X resolution:<input id="const_xres"></input>
</br>
</br>
Y resolution:<input id="const_yres"></input>
</br>
</br>
<input type="button" value="Load to Rom" onclick="load_program()"></input>
</br>
</br>
<b>ROM DUMP</b>
</br>
<textarea id="rom_log" rows="20" cols="40">
</textarea>
</br>
ROM start address:<input id="rom_dump_start"></input>
</br>
ROM end address:<input id="rom_dump_end"></input>
</br>
<input type="button" value="ROM Dump" onclick="rom_dump()">
</br>
</br>
</br>
<b>WORDS: </b><input readonly id="word_count_label"></input>
</br>
</br>
<b>Stack Dump</b>
</br>
<textarea id="stack_log" rows="20" cols="40">
</textarea>
</br>
<input type="button" value="Dump Stack" onclick="stack_dump()"></input>
</br>
</td>
</tr>
</table>
<!-- END DEBUGGER -->
</body>
</html>