996
|
1 /*
|
|
2 * Copyright (C) 2005 Gregor Richards
|
|
3 *
|
|
4 * This file is part of EgoBCh.
|
|
5 *
|
|
6 * EgoBCh is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * EgoBCh is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with EgoBCh; if not, write to the Free Software
|
|
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
19 */
|
|
20
|
|
21 #include <math.h>
|
|
22 #include <stdio.h>
|
|
23
|
|
24 #include "bchi.h"
|
|
25 #include "egobch.h"
|
|
26
|
|
27 #define LOGTWO 0.693147
|
|
28 #define SETBIT(a) (7 - (int) (log((double) (a)) / LOGTWO))
|
|
29
|
|
30 void binaryo()
|
|
31 {
|
|
32 int i, j;
|
|
33 unsigned char vbit;
|
|
34 unsigned char *curmem;
|
|
35 for (i = -2; i <= 2; i++) {
|
|
36 curmem = mptr + i;
|
|
37 if (curmem < mem) continue;
|
|
38 for (j = 7; j >= 0; j--) {
|
|
39 vbit = 1 << j;
|
|
40 if (i == 0 && vbit == membit)
|
|
41 putchar('*');
|
|
42 if (*curmem & vbit)
|
|
43 putchar('1');
|
|
44 else
|
|
45 putchar('0');
|
|
46 }
|
|
47 }
|
|
48 putchar('\n');
|
|
49 }
|
|
50
|
|
51 void bch_interpret()
|
|
52 {
|
|
53 int input, shft;
|
|
54 unsigned char toout;
|
|
55 struct bchi *cur;
|
|
56
|
|
57 while (1) {
|
|
58 cur = prog + pptr;
|
|
59 /*printf("%d %d %x\n", mptr - mem, SETBIT(membit), (unsigned int) *mptr);*/
|
|
60
|
|
61 switch (cur->cmd) {
|
|
62 case RTGL:
|
|
63 /* 1) move right */
|
|
64 membit >>= 1;
|
|
65 if (membit == 0) {
|
|
66 membit = 0x80;
|
|
67 mptr++;
|
|
68 }
|
|
69
|
|
70 /* 2) check for I/O */
|
|
71 if (mptr == mem && membit == 0x04) {
|
|
72 /* 2a) check whether to do input or output */
|
|
73 if (*mem & 0x01) {
|
|
74 /* 2ai) output */
|
|
75 putchar(mem[1]);
|
|
76 fflush(stdout);
|
|
77 } else {
|
|
78 /* 2aii) input */
|
|
79 input = getchar();
|
|
80 /* 2aiii) check for EOF */
|
|
81 if (input == EOF) {
|
|
82 /* mark it */
|
|
83 *mem |= 0x02;
|
|
84 } else {
|
|
85 mem[1] = (char) input;
|
|
86 }
|
|
87 }
|
|
88 } else {
|
|
89 /* 3) toggle */
|
|
90 *mptr ^= membit;
|
|
91 }
|
|
92 pptr++;
|
|
93 break;
|
|
94
|
|
95 case LFT:
|
|
96 /* move left */
|
|
97 membit <<= 1;
|
|
98 if (membit == 0) {
|
|
99 membit = 0x01;
|
|
100 mptr--;
|
|
101 }
|
|
102 pptr++;
|
|
103 break;
|
|
104
|
|
105 case LPO:
|
|
106 /* loop opening */
|
|
107 if (*mptr & membit) {
|
|
108 /* go into the loop */
|
|
109 pptr++;
|
|
110 } else {
|
|
111 /* jump the loop */
|
|
112 pptr = cur->arg1 + 1;
|
|
113 }
|
|
114 break;
|
|
115
|
|
116 case LPC:
|
|
117 /* loop close, jump to the beginning */
|
|
118 pptr = cur->arg1;
|
|
119 break;
|
|
120
|
|
121 case OUT:
|
|
122 /* lazy_io output */
|
|
123 shft = SETBIT(membit);
|
|
124 toout = *mptr << shft;
|
|
125
|
|
126 shft = 8 - shft;
|
|
127 toout |= mptr[1] >> shft;
|
|
128
|
|
129 putchar(toout);
|
|
130 fflush(stdout);
|
|
131 pptr++;
|
|
132 break;
|
|
133
|
|
134 case INP:
|
|
135 /* lazy_io input */
|
|
136 input = getchar();
|
|
137 if (input == EOF) input = 0;
|
|
138 toout = (char) input;
|
|
139
|
|
140 /* shift it in */
|
|
141 shft = SETBIT(membit);
|
|
142 *mptr &= ~(0xFF >> shft);
|
|
143 *mptr |= toout >> shft;
|
|
144
|
|
145 shft = 8 - shft;
|
|
146 mptr[1] &= ~(0xFF << shft);
|
|
147 mptr[1] |= toout << shft;
|
|
148 pptr++;
|
|
149 break;
|
|
150
|
|
151 case DBG:
|
|
152 binaryo();
|
|
153 pptr++;
|
|
154 break;
|
|
155
|
|
156 case FIN:
|
|
157 return;
|
|
158
|
|
159 default:
|
|
160 pptr++;
|
|
161 }
|
|
162 }
|
|
163 }
|