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