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 MOV (x, tmp)
181 #undef tmp
182 AND (t, x)
183 ADD (acc, t)
184
185 //voice 4:
186 MOV (Ml, i2)
187 SWAP (Ml)
188 ANDI (Ml, 0xf0)
189 LSL (Ml)
190 LSL (Ml)
191 #define tmp _
192 MOV (tmp, i1)
193 LSR (tmp)
194 LSR (tmp)
195 OR (Ml, tmp)
196 #undef tmp
197 MOV (Mh, i3)
198 SWAP (Mh)
199 ANDI (Mh, 0xf0)
200 LSL (Mh)
201 LSL (Mh)
202 #define tmp _
203 MOV (tmp, i2)
204 LSR (tmp)
205 LSR (tmp)
206 OR (Mh, tmp)
207 #undef tmp
208 RCALL mod3();
209 SUB (t, n)
210 NEG (t)
211 SUBI (t, -8)
212 RCALL g();
213 LSR (t)
214 ANDI (t, 3)
215 MOV (x, s)
216 INC (x)
217 #define tmp _
218 MOV (tmp, x)
219 LSR (tmp)
220 ADD (tmp, x)
221 ROR (tmp)
222 LSR (tmp)
223 LSR (tmp)
224 ADD (tmp, x)
225 ROR (tmp)
226 ADD (tmp, x)
227 ROR (tmp)
228 LSR (tmp)
229 LSR (tmp)
230 MOV (x, tmp)
231 #undef tmp
232 AND (t, x)
233 ADD (acc, t)
234
235 putchar(acc<<4); //TODO
236 SUBI (i0, -1)
237 ADC (i1, zero, !i0)
238 ADC (i2, zero, !i0&&!i1)
239 ADC (i3, zero, !i0&&!i1&&!i2)
240 }
241 }
Imprint / Impressum