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 i0;
10 u8 i1;
11 u8 i2;
12 u8 i3;
13 u8 x;
14 u8 t;
15 u8 o;
16 void g(void) {
17 // g(i, x, t, o) -> t
18 u8 tmp;
19 ANDI (t, 0x07)
20 MOV (tmp, i2)
21 ANDI (tmp, 3)
22 TST (tmp)
23 BREQ (skip)
24 SUBI (t, -8)
25 skip:
26 t = data[t];
27 t = (((i1&0x1f)<<8|i0)*t)>>8 >> o;
28 AND (t, x)
29 ANDI (t, 3)
30 RET
31 };
32
33 int main(void) {
34 u8 n;
35 u8 s;
36 u8 acc;
37 //TODO: clear all vars/registers
38 for (;;) {
39 MOV (n, i2)
40 LSL (n)
41 LSL (n)
42 #define tmp acc
43 MOV (tmp, i1)
44 SWAP (tmp)
45 ANDI (tmp, 0x0f)
46 LSR (tmp)
47 LSR (tmp)
48 OR (n, tmp)
49 #undef tmp
50 MOV (s, i3)
51 ROR (s)
52 ROR (s)
53 ANDI (s, 0x80)
54 #define tmp acc
55 MOV (tmp, i2)
56 LSR (tmp)
57 OR (s, tmp)
58 #undef tmp
59
60 //voice 1:
61 LDI (x, 1)
62 MOV (t, n)
63 LDI (o, 4)
64 RCALL g();
65 MOV (acc, t)
66
67 //voice 2:
68 MOV (x, s)
69 #define tmp o
70 MOV (tmp, i2)
71 LSL (tmp)
72 LSL (tmp)
73 LSL (tmp)
74 MOV (t, i1)
75 SWAP (t)
76 ANDI (t, 0xf)
77 LSR (t)
78 OR (t, tmp)
79 #undef tmp
80 EOR (t, n)
81 LDI (o, 2)
82 RCALL g();
83 ADD (acc, t)
84
85 //voice 3:
86 MOV (x, s)
87 INC (x)
88 #define tmp o
89 //NOTE: we only need 1 bit of the upper byte. instead of using a u16 and LSR'ing,
90 //we can use a u8 for sum and use ROR (which shifts the carry in)
91 CLR (tmp)
92 ADD (tmp, x)
93 ROR (tmp)
94 //nop
95 LSR (tmp)
96 ADD (tmp, x)
97 ROR (tmp)
98 //nop
99 LSR (tmp)
100 ADD (tmp, x)
101 ROR (tmp)
102 //nop
103 LSR (tmp)
104 ADD (tmp, x)
105 ROR (tmp)
106 //nop
107 LSR (tmp)
108 x = tmp;
109 #undef tmp
110 t = ((i3&0x01)<<13 | i2<<5 | i1>>3) % 3;
111 ADD (t, n)
112 LDI (o, 2)
113 RCALL g();
114 ADD (acc, t)
115
116 //voice 4:
117 MOV (x, s)
118 INC (x)
119 #define tmp o
120 unsigned short sum = 0; //XXX
121 sum += x;
122 sum >>= 1;
123 sum += x;
124 sum >>= 1;
125 //nop
126 sum >>= 1;
127 //nop
128 sum >>= 1;
129 sum += x;
130 sum >>= 1;
131 sum += x;
132 sum >>= 1;
133 //nop
134 sum >>= 1;
135 //nop
136 sum >>= 1;
137 x = sum;
138 #undef tmp
139 t = ((i3&0x01)<<14 | i2<<6 | i1>>2) % 3;
140 SUB (t, n)
141 NEG (t)
142 SUBI (t, -8)
143 LDI (o, 1)
144 RCALL g();
145 ADD (acc, t)
146
147 putchar(acc<<4);
148 #define tmp acc
149 LDI (tmp, 0)
150 SUBI (i0, -1)
151 ADC (i1, tmp, !i0)
152 ADC (i2, tmp, !i0&&!i1)
153 ADC (i3, tmp, !i0&&!i1&&!i2)
154 #undef tmp
155 }
156 }
Imprint / Impressum