new version
[Chiptunes.git] / foo.c
CommitLineData
61fab018 1#include <stdio.h>
da32ed67 2#include "fakeasm.h"
61fab018 3typedef unsigned char u8;
da32ed67 4
24abdcbb
TG
5u8 data[] = {
6 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58,
7 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
8};
61592bdd
TG
9u8 zero; //r16
10u8 acc; //r17
11u8 i0; //r18
12u8 i1; //r19
13u8 i2; //r20
14u8 i3; //r21
15u8 n; //r22
16u8 s; //r23
17u8 _; //r24
18 //r25
19u8 t;/*==Ml*/ //r26 (Xlo)
20u8 x;/*==Mh*/ //r27 (Xhi)
21 //r28
22 //r29
23/*fakestack_l*/ //r30 (Zlo)
24/*fakestack_h*/ //r31 (Zhi)
37bf20ea 25#define Mh x //mod3 vars
dbf91c38 26#define Ml t // -"-
e98ab46f 27//http://homepage.divms.uiowa.edu/~jones/bcd/mod.shtml
8d8c00e4
TG
28void mod3(void) {
29 // mod3(Mh.Ml) -> t
30 #define tmp _
5b1c6cc5
TG
31 ADD (Ml, Mh)
32 CLR (Mh)
3d517d8a
TG
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)
0e3d0279 39 ANDI (Ml, 0x0f)
2a69999d 40 ADD (Ml, tmp)
0fc1d6d3
TG
41 MOV (tmp, Ml)
42 LSR (tmp)
43 LSR (tmp)
6c72d3c1 44 ANDI (Ml, 0x03)
2a69999d
TG
45 ADD (Ml, tmp)
46 MOV (tmp, Ml)
47 LSR (tmp)
48 LSR (tmp)
49 ANDI (Ml, 0x03)
50 ADD (Ml, tmp)
c3639d5b
TG
51 CPI (Ml, 3)
52 BRPL (skip)
197a5418 53 SUBI (Ml, 3)
c3639d5b 54 skip:;
4283632d 55 RET
8d8c00e4 56 #undef tmp
e98ab46f 57}
965274e2 58void g(void) {
362b33c9 59 // g(i, t) -> t
eafeaf93 60 // tempvars: `x` and `_`
49137fbf 61 #define tmp _
0f219114 62 ANDI (t, 0x07)
32632e61 63 MOV (tmp, i2)
63363195 64 ANDI (tmp, 3)
09cf3949 65 TST (tmp)
49137fbf 66 #undef tmp
09cf3949
TG
67 BREQ (skip)
68 SUBI (t, -8)
69 skip:
c616f0c2 70 t = data[t];
8ee3310e 71 /*MOV X_hi==x, data_hi
49137fbf
TG
72 MOV X_lo==t, data_lo
73 ADD X_lo, t
49137fbf
TG
74 ADC X_hi, zero
75 LD t, X */
d397f897
TG
76 //t = (((i1&0x1f)<<8|i0)*t)>>8; //TODO
77
5700437a
TG
78 #define a1 x
79 #define a2 _
80 a2 = 0;
81 a1 = 0;
d397f897
TG
82 #define a0 t
83
84 for (u8 loop = 0; loop < 8; loop++) { //Note: t&2 always zero
7b96a97d
TG
85 SBRS (t, 0)
86 goto skip2;
d397f897 87 if (t & 1) {
84122650
TG
88 ADD (a1, i0)
89 ADC (a2, i1, carry)
d397f897 90 }
7b96a97d 91 skip2:
76d366f4
TG
92 LSR (a2)
93 ROR (a1)
94 ROR (t)
d397f897
TG
95 }
96 t = a1;
5700437a 97 #undef a0
d397f897 98
d5b74a12 99 RET //TODO: replace CALL/RET with IJMP?
61fab018
TG
100};
101
102int main(void) {
23e66ca4
TG
103 CLR (zero)
104 CLR (i0)
105 CLR (i1)
106 CLR (i2)
107 CLR (i3)
5dd8b8ff 108 for (;;) {
7874ed03
TG
109 MOV (n, i2)
110 LSL (n)
111 LSL (n)
8ee3310e 112 #define tmp _
bc7680e3 113 MOV (tmp, i1)
5d4207f9
TG
114 SWAP (tmp)
115 ANDI (tmp, 0x0f)
3eef1ade
TG
116 LSR (tmp)
117 LSR (tmp)
128ff01a 118 OR (n, tmp)
bc7680e3 119 #undef tmp
df192822 120 MOV (s, i3)
2bbe001f 121 LSR (s)
27b03017
TG
122 ROR (s)
123 ANDI (s, 0x80)
8ee3310e 124 #define tmp _
a582bbc3
TG
125 MOV (tmp, i2)
126 LSR (tmp)
e389879f 127 OR (s, tmp)
df192822 128 #undef tmp
3b86ca43
TG
129
130 //voice 1:
3b86ca43 131 MOV (t, n)
965274e2 132 RCALL g();
c09a6ed8 133 SWAP (t)
9e62fea4 134 ANDI (t, 1)
46a8d83c 135 MOV (acc, t)
3b86ca43
TG
136
137 //voice 2:
37bf20ea 138 #define tmp _
94c4920f
TG
139 MOV (tmp, i2)
140 LSL (tmp)
141 LSL (tmp)
142 LSL (tmp)
143 MOV (t, i1)
4b0b7dc5
TG
144 SWAP (t)
145 ANDI (t, 0xf)
146 LSR (t)
94c4920f 147 OR (t, tmp)
1b023e92 148 #undef tmp
23872091 149 EOR (t, n)
965274e2 150 RCALL g();
7716b427
TG
151 LSR (t)
152 LSR (t)
153 ANDI (t, 3)
f28def6a 154 AND (t, s)
46a8d83c 155 ADD (acc, t)
3b86ca43
TG
156
157 //voice 3:
500692e4
TG
158 MOV (Ml, i2)
159 SWAP (Ml)
160 ANDI (Ml, 0xf0)
161 LSL (Ml)
8ee3310e 162 #define tmp _
500692e4
TG
163 MOV (tmp, i1)
164 LSR (tmp)
165 LSR (tmp)
166 LSR (tmp)
167 OR (Ml, tmp)
168 #undef tmp
d39a46f5
TG
169 MOV (Mh, i3)
170 SWAP (Mh)
171 ANDI (Mh, 0xf0)
172 LSL (Mh)
173 #define tmp _
174 MOV (tmp, i2)
175 LSR (tmp)
176 LSR (tmp)
177 LSR (tmp)
178 OR (Mh, tmp)
179 #undef tmp
dbf91c38 180 RCALL mod3();
18570947 181 ADD (t, n)
965274e2 182 RCALL g();
c6c6cbe5
TG
183 LSR (t)
184 LSR (t)
185 ANDI (t, 3)
f28def6a
TG
186 MOV (x, s)
187 INC (x)
37bf20ea 188 #define tmp _
f28def6a
TG
189 MOV (tmp, x)
190 LSR (tmp)
191 LSR (tmp)
192 ADD (tmp, x)
193 ROR (tmp)
194 LSR (tmp)
195 ADD (tmp, x)
196 ROR (tmp)
197 LSR (tmp)
198 ADD (tmp, x)
199 ROR (tmp)
200 LSR (tmp)
51f43293 201 AND (t, tmp)
f28def6a 202 #undef tmp
46a8d83c 203 ADD (acc, t)
3b86ca43
TG
204
205 //voice 4:
649bb224
TG
206 MOV (Ml, i2)
207 SWAP (Ml)
208 ANDI (Ml, 0xf0)
209 LSL (Ml)
210 LSL (Ml)
8ee3310e 211 #define tmp _
649bb224
TG
212 MOV (tmp, i1)
213 LSR (tmp)
214 LSR (tmp)
215 OR (Ml, tmp)
216 #undef tmp
18426c43
TG
217 MOV (Mh, i3)
218 SWAP (Mh)
219 ANDI (Mh, 0xf0)
220 LSL (Mh)
221 LSL (Mh)
222 #define tmp _
223 MOV (tmp, i2)
224 LSR (tmp)
225 LSR (tmp)
226 OR (Mh, tmp)
227 #undef tmp
dbf91c38 228 RCALL mod3();
e4f7baf0
TG
229 SUB (t, n)
230 NEG (t)
902cfdea 231 SUBI (t, -8)
965274e2 232 RCALL g();
c6c6cbe5
TG
233 LSR (t)
234 ANDI (t, 3)
9548359d 235 INC (s)
37bf20ea 236 #define tmp _
9548359d 237 MOV (tmp, s)
d8af0686 238 LSR (tmp)
9548359d 239 ADD (tmp, s)
d8af0686
TG
240 ROR (tmp)
241 LSR (tmp)
242 LSR (tmp)
9548359d 243 ADD (tmp, s)
d8af0686 244 ROR (tmp)
9548359d 245 ADD (tmp, s)
d8af0686
TG
246 ROR (tmp)
247 LSR (tmp)
248 LSR (tmp)
51f43293 249 AND (t, tmp)
d8af0686 250 #undef tmp
46a8d83c 251 ADD (acc, t)
bfce2f8c 252
95fa231f 253 putchar(acc<<4); //TODO
89f35588 254 SUBI (i0, -1)
95fa231f
TG
255 ADC (i1, zero, !i0)
256 ADC (i2, zero, !i0&&!i1)
257 ADC (i3, zero, !i0&&!i1&&!i2)
fe9a76e4 258 }
61fab018 259}
Imprint / Impressum