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 = ((i3<<24|i2<<16|i1<<8|i0)*t) >> 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 #define tmp acc
51 MOV (s, i3)
52 ROR (s)
53 ROR (s)
54 ANDI (s, 0x80)
55 s = (s | i2>>1);
56 #undef tmp
57
58 //voice 1:
59 LDI (x, 1)
60 MOV (t, n)
61 LDI (o, 12)
62 RCALL g();
63 MOV (acc, t)
64
65 //voice 2:
66 MOV (x, s)
67 #define tmp o
68 MOV (tmp, i2)
69 LSL (tmp)
70 LSL (tmp)
71 LSL (tmp)
72 MOV (t, i1)
73 SWAP (t)
74 ANDI (t, 0xf)
75 LSR (t)
76 OR (t, tmp)
77 #undef tmp
78 EOR (t, n)
79 LDI (o, 10)
80 RCALL g();
81 ADD (acc, t)
82
83 //voice 3:
84 x = s / 3;
85 t = n + ((i3<<13 | i2<<5 | i1>>3) % 3);
86 LDI (o, 10)
87 RCALL g();
88 ADD (acc, t)
89
90 //voice 4:
91 x = s / 5;
92 t = 8 + n - ((i3<<14 | i2<<6 | i1>>2) % 3);
93 LDI (o, 9)
94 RCALL g();
95 ADD (acc, t)
96
97 putchar(acc<<4);
98 #define tmp acc
99 LDI (tmp, 0)
100 SUBI (i0, -1)
101 ADC (i1, tmp, !i0)
102 ADC (i2, tmp, !i0&&!i1)
103 ADC (i3, tmp, !i0&&!i1&&!i2)
104 #undef tmp
105 }
106 }
Imprint / Impressum