2 <title>Turing Tarpit Simulator
</title>
5 /* internal values for simulated computer */
6 var ins_pointer
= 0; /* instruction pointer */
7 var dat_pointer
= 0; /* data pointer */
8 var dat_pointer_changed
= 0; /* bool set to 1 if data pointer has changed in the last step */
9 var dat_pointer_last_address
= 0; /* address of data pointer in last step */
10 var ram_last_address
= 0; /* address of RAM updated in last step */
11 var breakpoints
= 1; /* bool set to 0 to ignore breakpoints */
12 var ram
= new Array();
14 /* objects that display data to the user */
18 var disp_pointer
= "<div id=disp_pointer>⇒</div>"; /* data pointer displayed in memory block */
20 /* highlight current instruction running on machine */
21 function code_highlight(inputEl
, selStart
, selEnd
) {
22 if (inputEl
.setSelectionRange
) {
24 inputEl
.setSelectionRange(selStart
, selEnd
);
25 } else if (inputEl
.createTextRange
) {
26 var range
= inputEl
.createTextRange();
28 range
.moveEnd('character', selEnd
);
29 range
.moveStart('character', selStart
);
34 /* turns breakpoints on and off */
35 function debug_toggle_breakpoints()
37 var button
= document
.getElementById("dbg_status");
40 button
.innerHTML
='<img src="http://opentextbook.info/icons/32x32/bug_delete.png">';
43 button
.innerHTML
='<img src="http://opentextbook.info/icons/32x32/bug_add.png">';
47 /* removes all breakpoints in source code */
48 function debug_remove_breakpoints()
50 /* ask user for permission first */
51 var ans
= confirm("Permanently remove all breakpoints from code?");
52 var code
= document
.getElementById("code");
53 if(ans
) { code
.value
= code
.value
.replace( /\*/g , '' ); }
56 /* execute next instruction from input */
57 function exe_nxt_inst()
59 var code
= document
.getElementById("code");
60 var ins
= null; /* current instruction from code */
61 if(ins_pointer
> code
.value
.length
) {
62 alert("HALT: Program is Complete");
65 ins
= code
.value
.substring(ins_pointer
-1,ins_pointer
); /* get data at ins_pointer location */
67 /* highlight code currently executing on screen */
68 code_highlight(code
, ins_pointer
-1, ins_pointer
);
73 ram_modify(dat_pointer
, 1);
76 ram_modify(dat_pointer
, -1);
84 case '*': /* these are comments - a break from the standard language */
85 if(breakpoints
) { stop(); }
91 /* must be a comment :) */
96 rewind code to beginning reset all memory locations
97 this simulates rebooting the computer
101 update_ins_pointer(0);
104 ram_highlight_clear();
105 dat_point_highlight();
106 document
.getElementById("output_disp").innerHTML
= '';
107 document
.getElementById("addr_0").innerHTML
= "0" + disp_pointer
;
110 /* stop stepping through code on timer if breakpoints enabled */
116 /* run through program on timer, stopping at breakpoints if enabled */
119 var total_steps
= document
.getElementById("code").value
.length
;
120 while(ins_pointer
<= total_steps
) { step_next(); }
123 /* step forward one instruction */
126 update_ins_pointer(1);
128 dat_point_highlight();
131 /* more back one instruction - undoes work of previous instruction */
134 update_ins_pointer(-1);
135 dat_point_highlight();
138 /* update instruction pointer
141 1 increment pointer by one
143 -1 decrement pointer by one
145 function update_ins_pointer(direction
)
147 if(direction
< 0 && ins_pointer
!= 0 ) { ins_pointer
--; }
148 else if (direction
== 0 ) { ins_pointer
= 0; }
149 else { ins_pointer
++; }
150 disp_ins_pointer
.value
= ins_pointer
;
153 /* setup virtual machine - must be called on page load */
156 disp_code
= document
.getElementById("code");
157 disp_ins_pointer
= document
.getElementById("ins_pointer_disp");
158 disp_ram
= document
.getElementById("ram_disp");
161 document
.getElementById("addr_0").innerHTML
= "0" + disp_pointer
;
162 /* reset_output(); */
165 /* reset all RAM to 0 */
169 for(i
=0; i
< 64; i
++) { ram
[i
] = 0; }
172 /* update background color of one memory address */
173 function ram_highlight(address
)
175 var disp_last_address
= document
.getElementById("ram_"+ram_last_address
);
176 if(address
%2==0) { /* will reset to original color of _stiped_ table */
177 document
.getElementById("ram_"+address
).style
.backgroundColor
= "#ff0";
179 document
.getElementById("ram_"+address
).style
.backgroundColor
= "#de0";
182 if(disp_last_address
) {
183 if(ram_last_address
%2==0) {
184 disp_last_address
.style
.backgroundColor
= "#fff";
186 disp_last_address
.style
.backgroundColor
= "#def";
191 /* remove highlight from all memory cells regardless of current status */
192 function ram_highlight_clear()
195 for(i
=0; i
< 64; i
++)
198 document
.getElementById("ram_"+i
).style
.backgroundColor
= "#fff";
200 document
.getElementById("ram_"+i
).style
.backgroundColor
= "#def";
202 document
.getElementById("ram_"+i
).innerHTML
= ram
[i
];
206 /* define RAM on screen */
210 var table
= "<table id=memory_table>\n";
211 for(i
=0; i
< 16; i
++){
215 table
+= "\t<tr class=odd>";
217 table
+= "<td width='35' class=addr id=addr_"+i
+">"+i
+"</td><td width='35' class=ram id=ram_"+i
+">";
218 table
+= ram
[i
]+"</td><td width='35' class=addr id=addr_"+(i
+16)+">";
219 table
+= (i
+16)+"</td><td class=ram id=ram_"+(i
+16)+">"+ram
[i
+16]+"</td>\n";
220 table
+= "<td class=addr id=addr_"+(i
+32)+">"+(i
+32)+"</td><td width='35' class=ram id=ram_";
221 table
+= (i
+32)+">"+ram
[i
+32]+"</td><td class=addr id=addr_";
222 table
+= (i
+48)+">"+(i
+48)+"</td><td width='35' class=ram id=ram_"+(i
+48)+">"+ram
[i
+48]+"</td></tr>\n";
224 table
+= "</table>\n";
225 disp_ram
.innerHTML
= table
;
228 /* modify the value at this address - reset RAM highlights for all addresses
229 1 increment RAM by one
231 -1 decrement RAM by one
233 function ram_modify(address
, change
)
235 if(address
< 0 || address
> 63) {
236 alert("SEGFAULT: Cannot Modify Data Outside of Memory Range");
242 } else if(change
< 0 ) {
247 ram_highlight(address
);
248 document
.getElementById("ram_"+address
).innerHTML
= ram
[address
];
249 ram_last_address
= address
;
252 /* modify state of data pointer */
253 function dat_point_modify(change
)
255 dat_pointer_last_address
= dat_pointer
;
258 } else if(change
< 0 ) {
259 /* could put SEGFAULT here, but C does not have a check for this, so neither will I ;) */
265 /* place '>' char next to memory address in table */
266 if(dat_pointer
>= 0 && dat_pointer
< 64) {
267 document
.getElementById("addr_"+dat_pointer
).innerHTML
= dat_pointer
+ disp_pointer
;
270 /* remove '>' char next to memory address in table */
271 if(dat_pointer_last_address
>= 0 && dat_pointer_last_address
< 64) {
272 document
.getElementById("addr_"+dat_pointer_last_address
).innerHTML
= dat_pointer_last_address
;
275 document
.getElementById("dat_pointer_disp").value
= dat_pointer
;
276 dat_pointer_changed
= 1;
279 /* turns on and off data pointer highlight if it has changed this step */
280 function dat_point_highlight()
282 var dat
= document
.getElementById("dat_pointer_disp");
283 //alert(dat_pointer_last_address+"\t"+dat_pointer);
284 if(dat_pointer_changed
) {
286 dat
.style
.backgroundColor
= "#ff0";
287 dat_pointer_changed
= 0;
290 dat
.style
.backgroundColor
= "#fff";
293 /* as a convenience to the user, set to red if pointer is out of memory range */
294 if(dat_pointer
< 0 || dat_pointer
> 63) {
295 dat
.style
.backgroundColor
= "#f00";
299 /* print ASCII representation of memory at data pointer */
302 var x
= String
.fromCharCode(ram
[dat_pointer
]);
303 document
.getElementById("output_disp").innerHTML
+= x
;
308 <style type=
"text/css">
311 font-family: Tahoma
, Arial
, Helvetica
, sans-serif
;
368 border:1px solid
#000;
369 border-collapse: collapse
;
374 border-bottom:1px solid
#000;
378 background-color: #def;
381 border-bottom: 1px solid
#cef;
389 border-right:1px solid
#000;
412 <body onLoad=
"init_form();">
414 <center><h2>Turing Tarpit Simulator
</h2></center>
417 <button onClick='reset();'
><img src=
"http://opentextbook.info/icons/32x32/resultset_first.png"></button>
419 <button onClick='step_back();'
><img src=
"http://opentextbook.info/icons/32x32/resultset_previous.png"></button>
421 <button onClick='step_next();'
><img src=
"http://opentextbook.info/icons/32x32/resultset_next.png"></button>
423 <button onClick='run();'
><img src=
"http://opentextbook.info/icons/32x32/resultset_last.png"></button>
425 <button onClick='reset();'
><img src=
"http://opentextbook.info/icons/32x32/cancel.png"></button>
427 <button disabled
><img src=
"http://opentextbook.info/icons/32x32/disk.png"></button>
430 <div id=
"code_buttons">
431 <button id=
"dbg_status" onClick='debug_toggle_breakpoints();'
>
432 <img src=
"http://opentextbook.info/icons/32x32/bug_add.png">
435 <button id=
"dbg_status" onClick='debug_remove_breakpoints();'
>
436 <img src=
"http://opentextbook.info/icons/32x32/bin.png">
440 <div id=
"input_banner">INPUT
</div>
441 <div id=
"input"><textarea id=
"code" rows=
"27" cols=
"38"></textarea></div>
443 <div id=
"ip_banner">INSTRUCTION POINTER
</div>
444 <div id=
"instruction"><input id=
"ins_pointer_disp" type=
"text" diabled value=
0></div>
446 <div id=
"da_banner">DATA POINTER
</div>
447 <div id=
"data"><input id=
"dat_pointer_disp" type=
"text" diabled value=
0></div>
449 <div id=
"memory_banner">MEMORY
</div>
450 <div id=
"ram_disp"></div>
451 <div id=
"output_banner">OUTPUT
</div>
453 <textarea name=code id=output_disp
rows=
"15" cols=
"38" disabled
></textarea>