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};
06aad1ff
TG
9u8 i0;
10u8 i1;
11u8 i2;
12u8 i3;
3b86ca43
TG
13u8 x;
14u8 t;
15u8 o;
965274e2 16void g(void) {
46a8d83c 17 // g(i, x, t, o) -> t
63363195 18 u8 tmp;
0f219114 19 ANDI (t, 0x07)
32632e61 20 MOV (tmp, i2)
63363195 21 ANDI (tmp, 3)
09cf3949
TG
22 TST (tmp)
23 BREQ (skip)
24 SUBI (t, -8)
25 skip:
c616f0c2 26 t = data[t];
9401049b 27 t = (((i1&0x1f)<<8|i0)*t)>>8 >> o;
c616f0c2
TG
28 AND (t, x)
29 ANDI (t, 3)
46a8d83c 30 RET
61fab018
TG
31};
32
33int main(void) {
a1631438
TG
34 u8 n;
35 u8 s;
ab6fe4c2 36 u8 acc;
89f35588 37 //TODO: clear all vars/registers
5dd8b8ff 38 for (;;) {
7874ed03
TG
39 MOV (n, i2)
40 LSL (n)
41 LSL (n)
bc7680e3
TG
42 #define tmp acc
43 MOV (tmp, i1)
5d4207f9
TG
44 SWAP (tmp)
45 ANDI (tmp, 0x0f)
3eef1ade
TG
46 LSR (tmp)
47 LSR (tmp)
128ff01a 48 OR (n, tmp)
bc7680e3 49 #undef tmp
df192822 50 MOV (s, i3)
27b03017
TG
51 ROR (s)
52 ROR (s)
53 ANDI (s, 0x80)
a582bbc3
TG
54 #define tmp acc
55 MOV (tmp, i2)
56 LSR (tmp)
e389879f 57 OR (s, tmp)
df192822 58 #undef tmp
3b86ca43
TG
59
60 //voice 1:
61 LDI (x, 1)
62 MOV (t, n)
9401049b 63 LDI (o, 4)
965274e2 64 RCALL g();
46a8d83c 65 MOV (acc, t)
3b86ca43
TG
66
67 //voice 2:
68 MOV (x, s)
1b023e92 69 #define tmp o
94c4920f
TG
70 MOV (tmp, i2)
71 LSL (tmp)
72 LSL (tmp)
73 LSL (tmp)
74 MOV (t, i1)
4b0b7dc5
TG
75 SWAP (t)
76 ANDI (t, 0xf)
77 LSR (t)
94c4920f 78 OR (t, tmp)
1b023e92 79 #undef tmp
23872091 80 EOR (t, n)
9401049b 81 LDI (o, 2)
965274e2 82 RCALL g();
46a8d83c 83 ADD (acc, t)
3b86ca43
TG
84
85 //voice 3:
2666c079
TG
86 MOV (x, s)
87 INC (x)
17c5b4e9 88 #define tmp o
43572923 89 unsigned short sum = 0; //XXX
43572923
TG
90 sum += x;
91 sum >>= 1;
92 //nop
93 sum >>= 1;
94 sum += x;
95 sum >>= 1;
96 //nop
97 sum >>= 1;
98 sum += x;
99 sum >>= 1;
100 //nop
b193bc64
TG
101 sum >>= 1;
102 sum += x;
103 sum >>= 1;
104 //nop
b193bc64
TG
105 sum >>= 1;
106 x = sum;
17c5b4e9 107 #undef tmp
18570947
TG
108 t = ((i3&0x01)<<13 | i2<<5 | i1>>3) % 3;
109 ADD (t, n)
9401049b 110 LDI (o, 2)
965274e2 111 RCALL g();
46a8d83c 112 ADD (acc, t)
3b86ca43
TG
113
114 //voice 4:
6bc3ca83
TG
115 MOV (x, s)
116 INC (x)
117 #define tmp o
43572923 118 sum = 0; //XXX
43572923
TG
119 sum += x;
120 sum >>= 1;
121 sum += x;
122 sum >>= 1;
123 //nop
124 sum >>= 1;
125 //nop
dc6579a6
TG
126 sum >>= 1;
127 sum += x;
128 sum >>= 1;
129 sum += x;
130 sum >>= 1;
131 //nop
132 sum >>= 1;
133 //nop
dc6579a6
TG
134 sum >>= 1;
135 x = sum;
6bc3ca83 136 #undef tmp
e4f7baf0
TG
137 t = ((i3&0x01)<<14 | i2<<6 | i1>>2) % 3;
138 SUB (t, n)
139 NEG (t)
902cfdea 140 SUBI (t, -8)
9401049b 141 LDI (o, 1)
965274e2 142 RCALL g();
46a8d83c 143 ADD (acc, t)
bfce2f8c 144
ab6fe4c2 145 putchar(acc<<4);
89f35588
TG
146 #define tmp acc
147 LDI (tmp, 0)
148 SUBI (i0, -1)
149 ADC (i1, tmp, !i0)
150 ADC (i2, tmp, !i0&&!i1)
151 ADC (i3, tmp, !i0&&!i1&&!i2)
dd7bbc4a 152 #undef tmp
fe9a76e4 153 }
61fab018 154}
Imprint / Impressum