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