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; //zero register
10 u8 i0;
11 u8 i1;
12 u8 i2;
13 u8 i3;
14 u8 x;
15 u8 t;
16 u8 o;
17 u8 _;
18 #define Mh o //mod3 vars
19 #define Ml t // -"-
20 void mod3(void) { //avail: t, o _
21 //http://homepage.divms.uiowa.edu/~jones/bcd/mod.shtml
22 #define tmp _
23 ADD (Ml, Mh)
24 CLR (Mh)
25 ADC (Mh, zero, carry) //Mh only holds the carry bit
26 MOV (tmp, Ml)
27 SWAP (tmp)
28 ANDI (tmp, 0x0f)
29 SWAP (Mh)
30 OR (tmp, Mh)
31 ANDI (Ml, 0x0f)
32 ADD (Ml, tmp) //discard tmp
33 MOV (tmp, Ml)
34 LSR (tmp)
35 LSR (tmp)
36 Ml = (tmp) + (Ml & 0x3);
37 Ml = (Ml >> 2) + (Ml & 0x3);
38 if (Ml > 2) Ml = Ml - 3;
39 #undef tmp
40 }
41 void g(void) {
42 // g(i, x, t, o) -> t
43 #define tmp _
44 ANDI (t, 0x07)
45 MOV (tmp, i2)
46 ANDI (tmp, 3)
47 TST (tmp)
48 #undef tmp
49 BREQ (skip)
50 SUBI (t, -8)
51 skip:
52 t = data[t];
53 /*MOV X_hi==_, data_hi
54 MOV X_lo==t, data_lo
55 ADD X_lo, t
56 ADC X_hi, zero
57 LD t, X */
58 t = (((i1&0x1f)<<8|i0)*t)>>8; //TODO
59 t >>= o; //NOTE: o == {1, 2, 4}
60 AND (t, x)
61 ANDI (t, 3)
62 RET
63 };
64
65 int main(void) {
66 u8 n;
67 u8 s;
68 u8 acc;
69 //TODO: clear all vars/registers
70 for (;;) {
71 MOV (n, i2)
72 LSL (n)
73 LSL (n)
74 #define tmp acc
75 MOV (tmp, i1)
76 SWAP (tmp)
77 ANDI (tmp, 0x0f)
78 LSR (tmp)
79 LSR (tmp)
80 OR (n, tmp)
81 #undef tmp
82 MOV (s, i3)
83 ROR (s)
84 ROR (s)
85 ANDI (s, 0x80)
86 #define tmp acc
87 MOV (tmp, i2)
88 LSR (tmp)
89 OR (s, tmp)
90 #undef tmp
91
92 //voice 1:
93 LDI (x, 1)
94 MOV (t, n)
95 LDI (o, 4)
96 RCALL g();
97 MOV (acc, t)
98
99 //voice 2:
100 MOV (x, s)
101 #define tmp o
102 MOV (tmp, i2)
103 LSL (tmp)
104 LSL (tmp)
105 LSL (tmp)
106 MOV (t, i1)
107 SWAP (t)
108 ANDI (t, 0xf)
109 LSR (t)
110 OR (t, tmp)
111 #undef tmp
112 EOR (t, n)
113 LDI (o, 2)
114 RCALL g();
115 ADD (acc, t)
116
117 //voice 3:
118 MOV (x, s)
119 INC (x)
120 #define tmp o
121 MOV (tmp, x)
122 LSR (tmp)
123 LSR (tmp)
124 ADD (tmp, x)
125 ROR (tmp)
126 LSR (tmp)
127 ADD (tmp, x)
128 ROR (tmp)
129 LSR (tmp)
130 ADD (tmp, x)
131 ROR (tmp)
132 LSR (tmp)
133 MOV (x, tmp)
134 #undef tmp
135 Ml = i2<<5 | i1>>3;
136 Mh = i3<<5 | i2>>3;
137 RCALL mod3();
138 ADD (t, n)
139 LDI (o, 2)
140 RCALL g();
141 ADD (acc, t)
142
143 //voice 4:
144 MOV (x, s)
145 INC (x)
146 #define tmp o
147 MOV (tmp, x)
148 LSR (tmp)
149 ADD (tmp, x)
150 ROR (tmp)
151 LSR (tmp)
152 LSR (tmp)
153 ADD (tmp, x)
154 ROR (tmp)
155 ADD (tmp, x)
156 ROR (tmp)
157 LSR (tmp)
158 LSR (tmp)
159 MOV (x, tmp)
160 #undef tmp
161 Ml = i2<<6 | i1>>2;
162 Mh = i3<<6 | i2>>2;
163 RCALL mod3();
164 SUB (t, n)
165 NEG (t)
166 SUBI (t, -8)
167 LDI (o, 1)
168 RCALL g();
169 ADD (acc, t)
170
171 putchar(acc<<4); //TODO
172 SUBI (i0, -1)
173 ADC (i1, zero, !i0)
174 ADC (i2, zero, !i0&&!i1)
175 ADC (i3, zero, !i0&&!i1&&!i2)
176 }
177 }
Imprint / Impressum