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 ROR (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 ANDI (t, 0x0f)
115 ANDI (t, 1)
116 MOV (acc, t)
117
118 //voice 2:
119 #define tmp _
120 MOV (tmp, i2)
121 LSL (tmp)
122 LSL (tmp)
123 LSL (tmp)
124 MOV (t, i1)
125 SWAP (t)
126 ANDI (t, 0xf)
127 LSR (t)
128 OR (t, tmp)
129 #undef tmp
130 EOR (t, n)
131 RCALL g();
132 LSR (t)
133 LSR (t)
134 ANDI (t, 3)
135 AND (t, s)
136 ADD (acc, t)
137
138 //voice 3:
139 MOV (Ml, i2)
140 SWAP (Ml)
141 ANDI (Ml, 0xf0)
142 LSL (Ml)
143 #define tmp _
144 MOV (tmp, i1)
145 LSR (tmp)
146 LSR (tmp)
147 LSR (tmp)
148 OR (Ml, tmp)
149 #undef tmp
150 MOV (Mh, i3)
151 SWAP (Mh)
152 ANDI (Mh, 0xf0)
153 LSL (Mh)
154 #define tmp _
155 MOV (tmp, i2)
156 LSR (tmp)
157 LSR (tmp)
158 LSR (tmp)
159 OR (Mh, tmp)
160 #undef tmp
161 RCALL mod3();
162 ADD (t, n)
163 RCALL g();
164 LSR (t)
165 LSR (t)
166 ANDI (t, 3)
167 MOV (x, s)
168 INC (x)
169 #define tmp _
170 MOV (tmp, x)
171 LSR (tmp)
172 LSR (tmp)
173 ADD (tmp, x)
174 ROR (tmp)
175 LSR (tmp)
176 ADD (tmp, x)
177 ROR (tmp)
178 LSR (tmp)
179 ADD (tmp, x)
180 ROR (tmp)
181 LSR (tmp)
182 MOV (x, tmp)
183 #undef tmp
184 AND (t, x)
185 ADD (acc, t)
186
187 //voice 4:
188 MOV (Ml, i2)
189 SWAP (Ml)
190 ANDI (Ml, 0xf0)
191 LSL (Ml)
192 LSL (Ml)
193 #define tmp _
194 MOV (tmp, i1)
195 LSR (tmp)
196 LSR (tmp)
197 OR (Ml, tmp)
198 #undef tmp
199 MOV (Mh, i3)
200 SWAP (Mh)
201 ANDI (Mh, 0xf0)
202 LSL (Mh)
203 LSL (Mh)
204 #define tmp _
205 MOV (tmp, i2)
206 LSR (tmp)
207 LSR (tmp)
208 OR (Mh, tmp)
209 #undef tmp
210 RCALL mod3();
211 SUB (t, n)
212 NEG (t)
213 SUBI (t, -8)
214 RCALL g();
215 LSR (t)
216 ANDI (t, 3)
217 MOV (x, s)
218 INC (x)
219 #define tmp _
220 MOV (tmp, x)
221 LSR (tmp)
222 ADD (tmp, x)
223 ROR (tmp)
224 LSR (tmp)
225 LSR (tmp)
226 ADD (tmp, x)
227 ROR (tmp)
228 ADD (tmp, x)
229 ROR (tmp)
230 LSR (tmp)
231 LSR (tmp)
232 MOV (x, tmp)
233 #undef tmp
234 AND (t, x)
235 ADD (acc, t)
236
237 putchar(acc<<4); //TODO
238 SUBI (i0, -1)
239 ADC (i1, zero, !i0)
240 ADC (i2, zero, !i0&&!i1)
241 ADC (i3, zero, !i0&&!i1&&!i2)
242 }
243 }
Imprint / Impressum