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