Check-in [f955c82a47]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:[] Updated to use for loop
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:f955c82a478b954f416f0c8953f458bc214fcbb6
User & Date: tthoma13 2016-01-20 00:36:18
Context
2016-01-20
00:36
[] Updated to use for loop Leaf check-in: f955c82a47 user: tthoma13 tags: trunk
2016-01-08
01:42
[] Initial Commit check-in: 708a921cdb user: tthoma13 tags: trunk
Changes

Added forth.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
<html>
<head><title>FORTH</title></head>
<body>
<canvas id="canvas" width="256" height="192"></canvas>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
function setPixel(imageData, x, y, r, g, b, a) {
    index = (x + y * imageData.width) * 4;
    imageData.data[index+0] = r;
    imageData.data[index+1] = g;
    imageData.data[index+2] = b;
    imageData.data[index+3] = a;
}

element = document.getElementById("canvas");
c = element.getContext("2d");

// read the width and height of the canvas
width = element.width;
height = element.height;
var color;

function setColor(tColor) {
   r = ((tColor >> 4) & 0x03);
   r = r * 85;
   g = ((tColor >> 2) & 0x03);
   g = g * 85;
   b = (tColor & 0x03);
   b = b * 85;
   color = [r,g,b];
}

function setPixel2(x, y) {
    setPixel(imageData, x, y, color[0], color[1], color[2], 255);
}

// create a new pixel array
imageData = c.createImageData(width, height);

setColor(0);

for( i = 0; i < 8; i++)
{
    setPixel2(i, 0);
    setPixel2(i, 1);
    setPixel2(3, i);
    setPixel2(4, i);
}

// copy the image data back onto the canvas
c.putImageData(imageData, 0, 0); // at coords 0,0

var VM_SIZE = (1024 * 64);
var CELL_SIZE = 2;
var SP0 = 6 * CELL_SIZE;
var RP0 = 7 * CELL_SIZE;
var SPP = 33 * CELL_SIZE;
var RPP = 34 * CELL_SIZE;
var TRUE = 0xFFFF;
var FALSE = 0;

var buffer = new ArrayBuffer(VM_SIZE);
var image = new Uint8Array(buffer, 0, buffer.length);

function GET(x){
    val = image[x];
    val = ((val << 8) + (image[x+1]));
    return val;
}

function SET(x, y){
     image[x] = ((y >> 8) & 0xFF);
     image[x+1] = (y & 0xFF);
}

function PUSHDS(x){
    SET(GET(SPP),x);
    SET(SPP,GET(SPP) + CELL_SIZE);
}

function POPDS(){
    SET(SPP,GET(SPP) - CELL_SIZE);
    return GET(SPP);
}

function PUSHRS(x){
    SET(GET(RPP),x);
    SET(RPP,GET(RPP) - CELL_SIZE);
}

function POPRS(){
    SET(RPP,GET(RPP) + CELL_SIZE);
    return GET(RPP);
}

function VM_RUN(start) {
    PC = start;
    regA = 0;
    regB = 0;
    regC = 0;
    PUSHRS(VM_SIZE - 1);
    while(PC < VM_SIZE - 1)
    {
    switch(GET(PC))
    {
        case 0: //NOP
            PC += CELL_SIZE;
            break;
        case 1: // HALT
            return PC;
        case 2: // LIT
            PC += CELL_SIZE;
            PUSHDS(GET(PC));
            PC += CELL_SIZE;
            break;
        case 3: // EXIT
            PC = POPRS();
            break;
        case 4: // @
            regA = POPDS();
            PUSHDS(GET(regA));
            PC += CELL_SIZE;
            break;
        case 5: // !
            POPDS(regA);
            POPDS(regB);
            SET(regA, regB);
            PC += CELL_SIZE;
            break;
        case 6: // DROP
            POPDS();
            PC += CELL_SIZE;
            break;
        case 7: // OVER
            regA = POPDS();
            regB = POPDS();
            PUSHDS(regB);
            PUSHDS(regA);
            PUSHDS(regB);
            PC += CELL_SIZE;
            break;
        case 8: // SWAP
            regA = POPDS();
            regB = POPDS();
            PUSHDS(regA);
            PUSHDS(regB);
            PC += CELL_SIZE;
            break;
        case 9: // DUP
            regA = POPDS();
            PUSHDS(regA);
            PUSHDS(regA);
            PC += CELL_SIZE;
            break;
        case 10: // UM+
            POPDS(regA);
            POPDS(regB);
            regC = regA + regB;
            if(regC > 0xFFFF) {
                PUSHDS(regC & 0xFFFF);
                PUSHDS(1);
            }
            else {
                PUSHDS(regC);
                PUSHDS(0);
            }
            PC += CELL_SIZE;
            break;
        case 11: // NAND
            regA = POPDS();
            regB = POPDS();
            PUSHDS(~(regA & regB) & 0xFFFF);
            PC += CELL_SIZE;
            break;
        case 12: // 0<
            regA = POPDS();
            if(regA > 0x8000) {
                PUSHDS(TRUE);
            }
            else { 
                PUSHDS(FALSE);
            }
            PC += CELL_SIZE;
            break;
        case 13: // >R
            regA = POPDS();
            PUSHRS(regA);
            PC += CELL_SIZE;
            break;
        case 14: // R>
            regA = POPRS();
            PUSHDS(regA);
            PC += CELL_SIZE;
            break;
        case 15: // next
            PC += CELL_SIZE;
            regA = GET(PC);
            POPRS(regB);
            regB = regB - 1;
            if(regB < 0) {
                PC += CELL_SIZE;
            }
            else {
                PUSHRS(regB);
                PC = regA;
            }
            break;
        case 16: // BRANCH
            PC += CELL_SIZE;
            PC = GET(PC);
            break;
        case 17: // 0BRANCH
            regA = POPDS();
            PC += CELL_SIZE;
            regB = GET(PC);
            if(regA == FALSE) {
                PC = regB;
            }
            else {
                PC += CELL_SIZE;
            }
            break;
        case 18: // TX!
            PC += CELL_SIZE;
            break;
        case 19: // ?RX
            PC += CELL_SIZE;
            break;
        case 20: // C@
            regA = POPDS();
            regB = image[regA];
            PUSHDS(regB);
            PC += CELL_SIZE;
            break;
        case 21: // C!
            regA = POPDS();
            regB = POPDS();
            image[regA] = regB;
            PC += CELL_SIZE;
            break;
        case 22: // plot
            regA = POPDS();
            regB = POPDS();
            setPixel(imageData, regA, regB, 0, 0, 0, 255);
            PC += CELL_SIZE;
            break;
        default: // CALL
            regA = PC;
            PUSHDS(GET(PC));
            PC += CELL_SIZE;
            PUSHRS(PC);
            PC = POPDS();
            break;
        }
    }
}

function vm_init(){
    for(x = 0;x < VM_SIZE;x++){
        image[x] = 0;
    }
    SET(SP0, 35 * CELL_SIZE);
    SET(SPP, 35 * CELL_SIZE);
    SET(RP0, 99 * CELL_SIZE);
    SET(RPP, 99 * CELL_SIZE);
}
//-->
</SCRIPT>
</body>
</html>