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