2 <title>Finite Automata
</title>
4 <b>Your browser does not support JavaScript or JavaScript is disabled.
</b>
15 var stage
= new Array(); /* status of stage - all details stored here */
16 var store
= new Array(); /* status of input */
18 var stage_rows
= 8; /* number of rows on stage */
19 var stage_cols
= 9; /* number of columns on stage */
20 var store_bit_count
= 10; /* number of bits stored in machine */
22 var input_count
= 10; /* number of input bits */
24 var grid_status
= 1; /* turn grid lines on and off */
25 var grid_size
= 50; /* size in pixels of grid lines - both X and Y */
26 var canvas
; /* object id of canvas tag */
27 var canvas_input
; /* object id of input tag */
29 var ctx
; /* context of canvas */
30 var input
; /* context of input canvas */
32 /* each square on grid has an associated block of data tied to it */
33 function grid(type
, bit
, dir
, col
){
34 this.type
= type
; /* type of machine part */
35 this.bit
= bit
; /* data in this block - if any */
36 this.dir
= dir
; /* direction this item is turned */
37 this.col
= col
; /* color of machine part (if any) */
40 /* this is used to display all bits on all active items */
41 function test_types(){
42 var i
= 0, j
= 0, k
= 1, l
= 2, col
= 1;
43 for(i
= 0; i
< stage_rows
; i
++) {
44 for(j
= 0; j
< stage_rows
; j
++){
47 case 1: stage
[i
][j
].type
= "branch"; break;
48 case 2: stage
[i
][j
].type
= "bus"; break;
49 case 3: stage
[i
][j
].type
= "input"; break;
50 case 4: stage
[i
][j
].type
= "output"; break;
52 stage
[i
][j
].type
= "bitadd";
53 if(col
== 5) { col
= 1; }
55 case 1: stage
[i
][j
].col
= RED
; break;
56 case 2: stage
[i
][j
].col
= GREEN
; break;
57 case 3: stage
[i
][j
].col
= YELLOW
; break;
58 case 4: stage
[i
][j
].col
= BLUE
; break;
66 case 1: stage
[i
][j
].bit
= RED
; break;
67 case 2: stage
[i
][j
].bit
= GREEN
; break;
68 case 3: stage
[i
][j
].bit
= YELLOW
; break;
69 case 4: stage
[i
][j
].bit
= BLUE
; break;
76 /* create all output data structures and draw inital values on screen */
77 function init_stage(){
80 /* create blank grid data structure */
81 for(x
= 0; x
< stage_cols
; x
++) {
82 stage
[x
] = new Array();
83 for(y
= 0; y
< stage_rows
; y
++) {
84 stage
[x
][y
] = new grid('', '', '', '');
87 stage
[5][0].type
= "input";
88 stage
[5][0].bit
= "red";
89 stage
[5][1].type
= "branch";
90 stage
[5][2].type
= "bus";
91 stage
[5][6].type
= "branch";
92 stage
[5][7].type
= "branch";
93 stage
[5][8].type
= "output";
95 stage
[0][1].type
= "bus";
98 stage
[0][2].type
= "bus";
100 stage
[0][3].type
= "bus";
102 stage
[0][4].type
= "bus";
109 /* set initial values for input */
110 function init_input(){
114 store
.push(GREEN
); /* add to end */
115 //store.unshift("red"); /* add to front */
116 //store.pop(); /* remove from end */
117 //store.shift(); /* remove from front */
120 /* draw faint gridlines on stage - used as a guide for the user */
121 function draw_grid(){
122 var x
, y
; /* current x and y position */
123 var offset
= 10; /* x and y maximum offset (far bottom or side of the window) */
124 ctx
.strokeStyle
= "#ccc";
126 /* draw vertical lines */
127 for(x
= grid_size
, y
= 0, offset
= window
.innerWidth
; x
< window
.innerWidth
; x
= x
+ grid_size
)
131 ctx
.lineTo(x
,y
+offset
);
135 /* draw horizontal lines */
136 for(x
= 0, y
= grid_size
, offset
= window
.innerWidth
; y
< window
.innerWidth
; y
= y
+ grid_size
)
140 ctx
.lineTo(x
+offset
,y
);
147 move through each grid in stage and draw contents.
148 this function can be used to refresh the screen at any time.
150 function draw_stage(){
152 /* loop through all grids on stage, drawing contents */
153 for(x
=0; x
< stage_cols
; x
++) {
154 for(y
= 0; y
< stage_rows
; y
++) {
160 function init_form(){
162 /* initalize canvas element for use */
163 canvas
= document
.getElementById("stage");
164 ctx
= canvas
.getContext("2d");
166 canvas_input
= document
.getElementById("input");
167 input
= canvas_input
.getContext("2d");
169 /* get width and height of window and set stage (canvas) with it. */
170 canvas
.height
= window
.innerHeight
-125;
171 canvas
.width
= window
.innerWidth
- 45;
172 if(grid_status
) {draw_grid(); }
179 /* returns coordinates of canvas in pixels */
180 function cnvs_get_coordinates(e
){
181 var x_offset
= canvas
.offsetLeft
;
182 var y_offset
= canvas
.offsetTop
;
183 if(canvas
== 'undefined') { alert("Canvas parameter is undefined"); }
184 x_offset
= e
.clientX
- x_offset
;
185 y_offset
= e
.clientY
- y_offset
;
186 document
.getElementById("xycoordinates").innerHTML
="Coordinates: (" + x_offset
+ "," + y_offset
+ ")";
187 return [x_offset
,y_offset
];
190 /* move through tape and draw bits */
191 function draw_tape(){
192 var i
= 0; var x
= 50;
193 input
.fillStyle
= "#f00";
194 while(i
< input_count
){
197 input
.fillStyle
= store
[i
];
198 input
.arc(x
,25,20,0,TWO_PI
,0);
201 input
.strokeStyle
= "#000";
204 input
.arc(x
,25,20,0,TWO_PI
,0);
212 /* (re)draws any map tile on grid */
213 function draw_tile(x
,y
){
215 ctx
.translate(grid_size
* x
, grid_size
* y
);
216 switch (stage
[x
][y
].type
){
218 draw_bitadd(stage
[x
][y
].col
);
224 draw_bus(stage
[x
][y
].dir
);
232 default: clear_square();
235 if(stage
[x
][y
].bit
){ draw_bit(stage
[x
][y
].bit
); }
239 /* draws small bit of correct color on grid */
240 function draw_bit(color
){
241 ctx
.fillStyle
= "#f00";
243 ctx
.fillStyle
= color
;
244 ctx
.arc(25,25,10,0,TWO_PI
,0);
247 ctx
.strokeStyle
= "#000";
250 ctx
.arc(25,25,10,0,TWO_PI
,0);
254 /* draw gray square with black outline */
255 function draw_input(){
257 ctx
.strokeStyle
= "#000";
258 ctx
.strokeRect(0,0,grid_size
,grid_size
);
259 ctx
.fillStyle
= "#aaa";
260 ctx
.fillRect(0,0,grid_size
,grid_size
);
263 /* a bus moves bits from one location to another */
264 function draw_bus(dir
){
267 ctx
.fillStyle
= "#aaa";
268 ctx
.strokeStyle
= "#000";
272 ctx
.translate(grid_size
,0);
276 ctx
.translate(grid_size
,grid_size
);
280 ctx
.translate(0,grid_size
);
286 if(i
== 1) { ctx
.save(); ctx
.translate(0, grid_size
/2); }
289 ctx
.lineTo(grid_size
/2,grid_size/2);
290 ctx
.lineTo(grid_size
,0);
291 ctx
.lineTo(grid_size
/2,grid_size/4);
297 ctx
.lineTo(grid_size
/2,grid_size/2);
298 ctx
.lineTo(grid_size
,0);
299 ctx
.lineTo(grid_size
/2,grid_size/4);
302 if(i
== 1) { ctx
.restore(); }
307 /* tiles branch movement of each bit */
308 function draw_branch(){
311 ctx
.fillStyle
= "#f00";
314 ctx
.lineTo(grid_size
/2,grid_size/2);
315 ctx
.lineTo(0,grid_size
);
320 ctx
.fillStyle
= "#000";
323 ctx
.lineTo(grid_size
/2,grid_size/2);
324 ctx
.lineTo(grid_size
,0);
330 ctx
.fillStyle
= "#00f";
332 ctx
.moveTo(grid_size
,0);
333 ctx
.lineTo(grid_size
/2,grid_size/2);
334 ctx
.lineTo(grid_size
,grid_size
);
339 ctx
.fillStyle
= "#aaa";
341 ctx
.moveTo(0,grid_size
)
342 ctx
.lineTo(grid_size
/2,grid_size/2);
343 ctx
.lineTo(grid_size
,grid_size
);
348 function draw_bitadd(color
){
352 ctx
.strokeStyle
= color
;
355 ctx
.strokeStyle
= "#000";
359 ctx
.moveTo(grid_size
/2,0);
360 ctx
.lineTo(grid_size
/2,grid_size
);
365 ctx
.moveTo(0, grid_size
/2);
366 ctx
.lineTo(grid_size
,grid_size
/2);
371 ctx
.strokeStyle
= "#000";
375 ctx
.lineTo(0,grid_size
);
376 ctx
.lineTo(grid_size
,grid_size
);
377 ctx
.lineTo(grid_size
,0);
383 /* clear this square by setting area to white */
384 function clear_square(){
385 ctx
.fillStyle
= "#fff";
386 ctx
.fillRect(1,1,grid_size
-2,grid_size
-2);
389 /* canvas has been clicked find out which grid and make correct change to square if needed. */
390 function cnvs_clicked(e
){}
394 <style type=
"text/css">
398 <body onLoad=
"init_form();">
400 <div id=
"topsection">
403 <button onClick='reset();'><img src="http://opentextbook.info/icons/32x32/resultset_first.png" title="Restart" alt="Restart"></button>
405 <button onClick='step_back();'><img src="http://opentextbook.info/icons/32x32/resultset_previous.png" title="Step Back" alt="Step Back"></button>
407 <button onClick='step_next();'><img src="http://opentextbook.info/icons/32x32/resultset_next.png" title="Next Step" alt="Next Step"></button>
409 <button onClick='run();'><img src="http://opentextbook.info/icons/32x32/resultset_last.png" title="Run" alt="Run"></button>
413 <button onClick='halt();'><img src="http://opentextbook.info/icons/32x32/cancel.png" title="Halt Execution" alt="Halt Execution"></button>
416 <button disabled><img src="http://opentextbook.info/icons/32x32/disk.png" title="Save Code" alt="Save Code"></button>
418 <button onClick='display_docs();'><img src="http://opentextbook.info/icons/32x32/book_open.png" title="Open Documentation" alt="Open Documentation"></button>
424 <div id=
"xycoordinates">Coordinates:
</div>
425 <canvas id=
"input" width=
"579" height=
"100"></canvas>
426 <canvas id=
"stage" width=
"579" height=
"770" onmousemove=
"cnvs_get_coordinates(event)" onclick=
"cnvs_clicked(event);">
427 Your browser does not support HTML5 Canvas.