new version
[Chiptunes.git] / foo.c
1 #include <stdio.h>
2 #include "fakeasm.h"
3 typedef unsigned char u8;
4
5 u8 data[] = {
6 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58,
7 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
8 };
9 u8 zero; //r16
10 u8 acc; //r17
11 u8 i0; //r18
12 u8 i1; //r19
13 u8 i2; //r20
14 u8 i3; //r21
15 u8 n; //r22
16 u8 s; //r23
17 u8 _; //r24
18 //r25
19 u8 t;/*==Ml*/ //r26 (Xlo)
20 u8 x;/*==Mh*/ //r27 (Xhi)
21 //r28
22 //r29
23 /*fakestack_l*/ //r30 (Zlo)
24 /*fakestack_h*/ //r31 (Zhi)
25 #define Mh x //mod3 vars
26 #define Ml t // -"-
27 //http://homepage.divms.uiowa.edu/~jones/bcd/mod.shtml
28 void mod3(void) {
29 // mod3(Mh.Ml) -> t
30 #define tmp _
31 ADD (Ml, Mh)
32 CLR (Mh)
33 ADC (Mh, zero, carry) //Mh only holds the carry bit
34 MOV (tmp, Ml)
35 SWAP (tmp)
36 ANDI (tmp, 0x0f)
37 SWAP (Mh)
38 OR (tmp, Mh)
39 ANDI (Ml, 0x0f)
40 ADD (Ml, tmp)
41 MOV (tmp, Ml)
42 LSR (tmp)
43 LSR (tmp)
44 ANDI (Ml, 0x03)
45 ADD (Ml, tmp)
46 MOV (tmp, Ml)
47 LSR (tmp)
48 LSR (tmp)
49 ANDI (Ml, 0x03)
50 ADD (Ml, tmp)
51 CPI (Ml, 3)
52 BRPL (skip)
53 SUBI (Ml, 3)
54 skip:;
55 RET
56 #undef tmp
57 }
58 void g(void) {
59 // g(i, t) -> t
60 // tempvars: `x` and `_`
61 #define tmp _
62 ANDI (t, 0x07)
63 MOV (tmp, i2)
64 ANDI (tmp, 3)
65 TST (tmp)
66 #undef tmp
67 BREQ (skip)
68 SUBI (t, -8)
69 skip:
70 t = data[t];
71 /*MOV X_hi==x, data_hi
72 MOV X_lo==t, data_lo
73 ADD X_lo, t
74 ADC X_hi, zero
75 LD t, X */
76 t &= 0xfd; //hint
77 t = (((i1&0x1f)<<8|i0)*t)>>8; //TODO
78 t &= 0x1e; //hint
79 RET //TODO: replace CALL/RET with IJMP?
80 };
81
82 int main(void) {
83 CLR (zero)
84 CLR (i0)
85 CLR (i1)
86 CLR (i2)
87 CLR (i3)
88 for (;;) {
89 MOV (n, i2)
90 LSL (n)
91 LSL (n)
92 #define tmp _
93 MOV (tmp, i1)
94 SWAP (tmp)
95 ANDI (tmp, 0x0f)
96 LSR (tmp)
97 LSR (tmp)
98 OR (n, tmp)
99 #undef tmp
100 MOV (s, i3)
101 LSR (s)
102 ROR (s)
103 ANDI (s, 0x80)
104 #define tmp _
105 MOV (tmp, i2)
106 LSR (tmp)
107 OR (s, tmp)
108 #undef tmp
109
110 //voice 1:
111 MOV (t, n)
112 RCALL g();
113 SWAP (t)
114 MOV (acc, t)
115
116 //voice 2:
117 #define tmp _
118 MOV (tmp, i2)
119 LSL (tmp)
120 LSL (tmp)
121 LSL (tmp)
122 MOV (t, i1)
123 SWAP (t)
124 ANDI (t, 0xf)
125 LSR (t)
126 OR (t, tmp)
127 #undef tmp
128 EOR (t, n)
129 RCALL g();
130 LSR (t)
131 LSR (t)
132 ANDI (t, 3)
133 AND (t, s)
134 ADD (acc, t)
135
136 //voice 3:
137 MOV (Ml, i2)
138 SWAP (Ml)
139 ANDI (Ml, 0xf0)
140 LSL (Ml)
141 #define tmp _
142 MOV (tmp, i1)
143 LSR (tmp)
144 LSR (tmp)
145 LSR (tmp)
146 OR (Ml, tmp)
147 #undef tmp
148 MOV (Mh, i3)
149 SWAP (Mh)
150 ANDI (Mh, 0xf0)
151 LSL (Mh)
152 #define tmp _
153 MOV (tmp, i2)
154 LSR (tmp)
155 LSR (tmp)
156 LSR (tmp)
157 OR (Mh, tmp)
158 #undef tmp
159 RCALL mod3();
160 ADD (t, n)
161 RCALL g();
162 LSR (t)
163 LSR (t)
164 ANDI (t, 3)
165 MOV (x, s)
166 INC (x)
167 #define tmp _
168 MOV (tmp, x)
169 LSR (tmp)
170 LSR (tmp)
171 ADD (tmp, x)
172 ROR (tmp)
173 LSR (tmp)
174 ADD (tmp, x)
175 ROR (tmp)
176 LSR (tmp)
177 ADD (tmp, x)
178 ROR (tmp)
179 LSR (tmp)
180 AND (t, tmp)
181 #undef tmp
182 ADD (acc, t)
183
184 //voice 4:
185 MOV (Ml, i2)
186 SWAP (Ml)
187 ANDI (Ml, 0xf0)
188 LSL (Ml)
189 LSL (Ml)
190 #define tmp _
191 MOV (tmp, i1)
192 LSR (tmp)
193 LSR (tmp)
194 OR (Ml, tmp)
195 #undef tmp
196 MOV (Mh, i3)
197 SWAP (Mh)
198 ANDI (Mh, 0xf0)
199 LSL (Mh)
200 LSL (Mh)
201 #define tmp _
202 MOV (tmp, i2)
203 LSR (tmp)
204 LSR (tmp)
205 OR (Mh, tmp)
206 #undef tmp
207 RCALL mod3();
208 SUB (t, n)
209 NEG (t)
210 SUBI (t, -8)
211 RCALL g();
212 LSR (t)
213 ANDI (t, 3)
214 #define x s //don't need `s` afterwards
215 INC (x)
216 #define tmp _
217 MOV (tmp, x)
218 LSR (tmp)
219 ADD (tmp, x)
220 ROR (tmp)
221 LSR (tmp)
222 LSR (tmp)
223 ADD (tmp, x)
224 ROR (tmp)
225 ADD (tmp, x)
226 ROR (tmp)
227 LSR (tmp)
228 LSR (tmp)
229 #undef x
230 AND (t, tmp)
231 #undef tmp
232 ADD (acc, t)
233
234 putchar(acc<<4); //TODO
235 SUBI (i0, -1)
236 ADC (i1, zero, !i0)
237 ADC (i2, zero, !i0&&!i1)
238 ADC (i3, zero, !i0&&!i1&&!i2)
239 }
240 }
Imprint / Impressum