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};
95fa231f 9u8 zero; //zero register
06aad1ff
TG
10u8 i0;
11u8 i1;
12u8 i2;
13u8 i3;
3b86ca43
TG
14u8 x;
15u8 t;
16u8 o;
49137fbf 17u8 _;
e98ab46f 18#define Mh o //mod3 vars
dbf91c38
TG
19#define Ml t // -"-
20void mod3(void) { //avail: t, o _
e98ab46f 21//http://homepage.divms.uiowa.edu/~jones/bcd/mod.shtml
5b1c6cc5 22 #define tmp _
5b1c6cc5
TG
23 ADD (Ml, Mh)
24 CLR (Mh)
3d517d8a
TG
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)
0e3d0279 31 ANDI (Ml, 0x0f)
2a69999d 32 ADD (Ml, tmp)
0fc1d6d3
TG
33 MOV (tmp, Ml)
34 LSR (tmp)
35 LSR (tmp)
6c72d3c1 36 ANDI (Ml, 0x03)
2a69999d
TG
37 ADD (Ml, tmp)
38 MOV (tmp, Ml)
39 LSR (tmp)
40 LSR (tmp)
41 ANDI (Ml, 0x03)
42 ADD (Ml, tmp)
dbf91c38 43 if (Ml > 2) Ml = Ml - 3;
5b1c6cc5 44 #undef tmp
e98ab46f 45}
965274e2 46void g(void) {
46a8d83c 47 // g(i, x, t, o) -> t
49137fbf 48 #define tmp _
0f219114 49 ANDI (t, 0x07)
32632e61 50 MOV (tmp, i2)
63363195 51 ANDI (tmp, 3)
09cf3949 52 TST (tmp)
49137fbf 53 #undef tmp
09cf3949
TG
54 BREQ (skip)
55 SUBI (t, -8)
56 skip:
c616f0c2 57 t = data[t];
49137fbf
TG
58 /*MOV X_hi==_, data_hi
59 MOV X_lo==t, data_lo
60 ADD X_lo, t
49137fbf
TG
61 ADC X_hi, zero
62 LD t, X */
e5715654
TG
63 t = (((i1&0x1f)<<8|i0)*t)>>8; //TODO
64 t >>= o; //NOTE: o == {1, 2, 4}
c616f0c2
TG
65 AND (t, x)
66 ANDI (t, 3)
46a8d83c 67 RET
61fab018
TG
68};
69
70int main(void) {
a1631438
TG
71 u8 n;
72 u8 s;
ab6fe4c2 73 u8 acc;
89f35588 74 //TODO: clear all vars/registers
5dd8b8ff 75 for (;;) {
7874ed03
TG
76 MOV (n, i2)
77 LSL (n)
78 LSL (n)
bc7680e3
TG
79 #define tmp acc
80 MOV (tmp, i1)
5d4207f9
TG
81 SWAP (tmp)
82 ANDI (tmp, 0x0f)
3eef1ade
TG
83 LSR (tmp)
84 LSR (tmp)
128ff01a 85 OR (n, tmp)
bc7680e3 86 #undef tmp
df192822 87 MOV (s, i3)
27b03017
TG
88 ROR (s)
89 ROR (s)
90 ANDI (s, 0x80)
a582bbc3
TG
91 #define tmp acc
92 MOV (tmp, i2)
93 LSR (tmp)
e389879f 94 OR (s, tmp)
df192822 95 #undef tmp
3b86ca43
TG
96
97 //voice 1:
98 LDI (x, 1)
99 MOV (t, n)
9401049b 100 LDI (o, 4)
965274e2 101 RCALL g();
46a8d83c 102 MOV (acc, t)
3b86ca43
TG
103
104 //voice 2:
105 MOV (x, s)
1b023e92 106 #define tmp o
94c4920f
TG
107 MOV (tmp, i2)
108 LSL (tmp)
109 LSL (tmp)
110 LSL (tmp)
111 MOV (t, i1)
4b0b7dc5
TG
112 SWAP (t)
113 ANDI (t, 0xf)
114 LSR (t)
94c4920f 115 OR (t, tmp)
1b023e92 116 #undef tmp
23872091 117 EOR (t, n)
9401049b 118 LDI (o, 2)
965274e2 119 RCALL g();
46a8d83c 120 ADD (acc, t)
3b86ca43
TG
121
122 //voice 3:
2666c079
TG
123 MOV (x, s)
124 INC (x)
17c5b4e9 125 #define tmp o
a7a7abba 126 MOV (tmp, x)
f84bcb7f 127 LSR (tmp)
546b5bab
TG
128 LSR (tmp)
129 ADD (tmp, x)
130 ROR (tmp)
546b5bab
TG
131 LSR (tmp)
132 ADD (tmp, x)
133 ROR (tmp)
546b5bab
TG
134 LSR (tmp)
135 ADD (tmp, x)
136 ROR (tmp)
546b5bab 137 LSR (tmp)
2c94c801 138 MOV (x, tmp)
17c5b4e9 139 #undef tmp
e98ab46f
TG
140 Ml = i2<<5 | i1>>3;
141 Mh = i3<<5 | i2>>3;
dbf91c38 142 RCALL mod3();
18570947 143 ADD (t, n)
9401049b 144 LDI (o, 2)
965274e2 145 RCALL g();
46a8d83c 146 ADD (acc, t)
3b86ca43
TG
147
148 //voice 4:
6bc3ca83
TG
149 MOV (x, s)
150 INC (x)
151 #define tmp o
86f35aa4
TG
152 MOV (tmp, x)
153 LSR (tmp)
154 ADD (tmp, x)
155 ROR (tmp)
86f35aa4 156 LSR (tmp)
86f35aa4
TG
157 LSR (tmp)
158 ADD (tmp, x)
159 ROR (tmp)
160 ADD (tmp, x)
161 ROR (tmp)
86f35aa4 162 LSR (tmp)
86f35aa4 163 LSR (tmp)
c2693411 164 MOV (x, tmp)
6bc3ca83 165 #undef tmp
e98ab46f
TG
166 Ml = i2<<6 | i1>>2;
167 Mh = i3<<6 | i2>>2;
dbf91c38 168 RCALL mod3();
e4f7baf0
TG
169 SUB (t, n)
170 NEG (t)
902cfdea 171 SUBI (t, -8)
9401049b 172 LDI (o, 1)
965274e2 173 RCALL g();
46a8d83c 174 ADD (acc, t)
bfce2f8c 175
95fa231f 176 putchar(acc<<4); //TODO
89f35588 177 SUBI (i0, -1)
95fa231f
TG
178 ADC (i1, zero, !i0)
179 ADC (i2, zero, !i0&&!i1)
180 ADC (i3, zero, !i0&&!i1&&!i2)
fe9a76e4 181 }
61fab018 182}
Imprint / Impressum