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