2 <title>Finite Automata
</title>
4 <b>Your browser does not support JavaScript or JavaScript is disabled.
</b>
10 var stage
= new Array(); /* status of stage - all details stored here */
11 var store
= new Array(); /* status of input */
13 var stage_rows
= 8; /* number of rows on stage */
14 var stage_cols
= 9; /* number of columns on stage */
15 var store_bit_count
= 10; /* number of bits stored in machine */
17 var input_count
= 10; /* number of input bits */
19 var grid_status
= 1; /* turn grid lines on and off */
20 var grid_size
= 50; /* size in pixels of grid lines - both X and Y */
21 var canvas
; /* object id of canvas tag */
22 var canvas_input
; /* object id of input tag */
24 var ctx
; /* context of canvas */
25 var input
; /* context of input canvas */
27 /* each square on grid has an associated block of data tied to it */
28 function grid(type
, bit
, dir
){
30 this.bit
= bit
; /* data in this block - if any */
31 this.dir
= dir
; /* direction this item is turned */
34 /* this is used to display all bits on all active items */
35 function test_types(){
36 var i
= 0, j
= 0, k
= 1, l
= 2;
37 for(i
= 0; i
< stage_rows
; i
++) {
38 for(j
= 0; j
< stage_rows
; j
++){
41 case 1: stage
[i
][j
].type
= "branch"; break;
42 case 2: stage
[i
][j
].type
= "bus"; break;
43 case 3: stage
[i
][j
].type
= "input"; break;
44 case 4: stage
[i
][j
].type
= "output"; break;
50 case 1: stage
[i
][j
].bit
= "red"; break;
51 case 2: stage
[i
][j
].bit
= "green"; break;
52 case 3: stage
[i
][j
].bit
= "yellow"; break;
53 case 4: stage
[i
][j
].bit
= "blue"; break;
60 /* create all output data structures and draw inital values on screen */
61 function init_stage(){
64 /* create blank grid data structure */
65 for(x
= 0; x
< stage_cols
; x
++) {
66 stage
[x
] = new Array();
67 for(y
= 0; y
< stage_rows
; y
++) {
68 stage
[x
][y
] = new grid('', '', '');
71 stage
[5][0].type
= "input";
72 stage
[5][0].bit
= "red";
73 stage
[5][1].type
= "branch";
74 stage
[5][2].type
= "bus";
75 stage
[5][6].type
= "branch";
76 stage
[5][7].type
= "branch";
77 stage
[5][8].type
= "output";
79 stage
[0][1].type
= "bus";
82 stage
[0][2].type
= "bus";
84 stage
[0][3].type
= "bus";
86 stage
[0][4].type
= "bus";
93 /* set initial values for input */
94 function init_input(){
98 store
.push("green"); /* add to end */
99 //store.unshift("red"); /* add to front */
100 //store.pop(); /* remove from end */
101 //store.shift(); /* remove from front */
104 /* draw faint gridlines on stage - used as a guide for the user */
105 function draw_grid(){
106 var x
, y
; /* current x and y position */
107 var offset
= 10; /* x and y maximum offset (far bottom or side of the window) */
108 ctx
.strokeStyle
= "#ccc";
110 /* draw vertical lines */
111 for(x
= grid_size
, y
= 0, offset
= window
.innerWidth
; x
< window
.innerWidth
; x
= x
+ grid_size
)
115 ctx
.lineTo(x
,y
+offset
);
119 /* draw horizontal lines */
120 for(x
= 0, y
= grid_size
, offset
= window
.innerWidth
; y
< window
.innerWidth
; y
= y
+ grid_size
)
124 ctx
.lineTo(x
+offset
,y
);
131 move through each grid in stage and draw contents.
132 this function can be used to refresh the screen at any time.
134 function draw_stage(){
136 /* loop through all grids on stage, drawing contents */
137 for(x
=0; x
< stage_cols
; x
++) {
138 for(y
= 0; y
< stage_rows
; y
++) {
144 function init_form(){
146 /* initalize canvas element for use */
147 canvas
= document
.getElementById("stage");
148 ctx
= canvas
.getContext("2d");
150 canvas_input
= document
.getElementById("input");
151 input
= canvas_input
.getContext("2d");
153 /* get width and height of window and set stage (canvas) with it. */
154 canvas
.height
= window
.innerHeight
-125;
155 canvas
.width
= window
.innerWidth
- 45;
156 if(grid_status
) {draw_grid(); }
163 /* returns coordinates of canvas in pixels */
164 function cnvs_get_coordinates(e
){
165 var x_offset
= canvas
.offsetLeft
;
166 var y_offset
= canvas
.offsetTop
;
167 if(canvas
== 'undefined') { alert("Canvas parameter is undefined"); }
168 x_offset
= e
.clientX
- x_offset
;
169 y_offset
= e
.clientY
- y_offset
;
170 document
.getElementById("xycoordinates").innerHTML
="Coordinates: (" + x_offset
+ "," + y_offset
+ ")";
171 return [x_offset
,y_offset
];
174 /* move through tape and draw bits */
175 function draw_tape(){
176 var i
= 0; var x
= 50;
177 input
.fillStyle
= "#f00";
178 while(i
< input_count
){
183 input
.fillStyle
= "#f00";
186 input
.fillStyle
= "#00f";
189 input
.fillStyle
= "#ff0";
192 input
.fillStyle
= "#0f0";
194 default: input
.fillStyle
= "#000";
196 input
.arc(x
,25,20,0,TWO_PI
,0);
199 input
.strokeStyle
= "#000";
202 input
.arc(x
,25,20,0,TWO_PI
,0);
210 /* (re)draws any map tile on grid */
211 function draw_tile(x
,y
){
213 ctx
.translate(grid_size
* x
, grid_size
* y
);
214 switch (stage
[x
][y
].type
){
219 draw_bus(stage
[x
][y
].dir
);
227 default: clear_square();
230 if(stage
[x
][y
].bit
){ draw_bit(stage
[x
][y
].bit
); }
234 /* draws small bit of correct color on grid */
235 function draw_bit(color
){
236 ctx
.fillStyle
= "#f00";
240 ctx
.fillStyle
= "#f00";
243 ctx
.fillStyle
= "#00f";
246 ctx
.fillStyle
= "#ff0";
249 ctx
.fillStyle
= "#0f0";
251 default: ctx
.fillStyle
= "#000";
253 ctx
.arc(25,25,10,0,TWO_PI
,0);
256 ctx
.strokeStyle
= "#000";
259 ctx
.arc(25,25,10,0,TWO_PI
,0);
263 /* draw gray square with black outline */
264 function draw_input(){
266 ctx
.strokeStyle
= "#000";
267 ctx
.strokeRect(2,2,grid_size
-4,grid_size
-4);
268 ctx
.fillStyle
= "#aaa";
269 ctx
.fillRect( 3 ,3 ,grid_size
-6,grid_size
-6);
272 /* a bus moves bits from one location to another */
273 function draw_bus(dir
){
276 ctx
.fillStyle
= "#aaa";
277 ctx
.strokeStyle
= "#000";
281 ctx
.translate(grid_size
,0);
285 ctx
.translate(grid_size
,grid_size
);
289 ctx
.translate(0,grid_size
);
295 if(i
== 1) { ctx
.save(); ctx
.translate(0, grid_size
/2); }
298 ctx
.lineTo(grid_size
/2,grid_size/2);
299 ctx
.lineTo(grid_size
,0);
300 ctx
.lineTo(grid_size
/2,grid_size/4);
306 ctx
.lineTo(grid_size
/2,grid_size/2);
307 ctx
.lineTo(grid_size
,0);
308 ctx
.lineTo(grid_size
/2,grid_size/4);
311 if(i
== 1) { ctx
.restore(); }
316 /* tiles branch movement of each bit */
317 function draw_branch(){
321 ctx
.fillStyle
= "#f00";
324 ctx
.lineTo(grid_size
/2,grid_size/2);
325 ctx
.lineTo(0,grid_size
);
330 ctx
.fillStyle
= "#000";
333 ctx
.lineTo(grid_size
/2,grid_size/2);
334 ctx
.lineTo(grid_size
,0);
340 ctx
.fillStyle
= "#00f";
342 ctx
.moveTo(grid_size
,0);
343 ctx
.lineTo(grid_size
/2,grid_size/2);
344 ctx
.lineTo(grid_size
,grid_size
);
349 ctx
.fillStyle
= "#aaa";
351 ctx
.moveTo(0,grid_size
)
352 ctx
.lineTo(grid_size
/2,grid_size/2);
353 ctx
.lineTo(grid_size
,grid_size
);
358 /* clear this square by setting area to white */
359 function clear_square(){
360 ctx
.fillStyle
= "#fff";
361 ctx
.fillRect(1,1,grid_size
-2,grid_size
-2);
364 /* canvas has been clicked find out which grid and make correct change to square if needed. */
365 function cnvs_clicked(e
){}
369 <style type=
"text/css">
373 <body onLoad=
"init_form();">
375 <div id=
"topsection">
378 <button onClick='reset();'><img src="http://opentextbook.info/icons/32x32/resultset_first.png" title="Restart" alt="Restart"></button>
380 <button onClick='step_back();'><img src="http://opentextbook.info/icons/32x32/resultset_previous.png" title="Step Back" alt="Step Back"></button>
382 <button onClick='step_next();'><img src="http://opentextbook.info/icons/32x32/resultset_next.png" title="Next Step" alt="Next Step"></button>
384 <button onClick='run();'><img src="http://opentextbook.info/icons/32x32/resultset_last.png" title="Run" alt="Run"></button>
388 <button onClick='halt();'><img src="http://opentextbook.info/icons/32x32/cancel.png" title="Halt Execution" alt="Halt Execution"></button>
391 <button disabled><img src="http://opentextbook.info/icons/32x32/disk.png" title="Save Code" alt="Save Code"></button>
393 <button onClick='display_docs();'><img src="http://opentextbook.info/icons/32x32/book_open.png" title="Open Documentation" alt="Open Documentation"></button>
399 <div id=
"xycoordinates">Coordinates:
</div>
400 <canvas id=
"input" width=
"579" height=
"100"></canvas>
401 <canvas id=
"stage" width=
"579" height=
"770" onmousemove=
"cnvs_get_coordinates(event)" onclick=
"cnvs_clicked(event);">
402 Your browser does not support HTML5 Canvas.