Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit 3f07c32

Browse files
authored
Merge pull request #1835 from reicast/skmp/aica-dsp-asm
DSP: Add assembler/disassembler
2 parents ad7452a + b67a530 commit 3f07c32

File tree

5 files changed

+392
-81
lines changed

5 files changed

+392
-81
lines changed

aica-dsp-asm/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
g++ main.cpp ../libswirl/hw/aica/dsp_helpers.cpp -I.. -I../libswirl -DTARGET_LINUX_x64 -o aica-dsp-asm

aica-dsp-asm/main.cpp

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
This is part of libswirl
3+
*/
4+
#include <license/bsd>
5+
6+
#include <vector>
7+
#include <map>
8+
#include <string>
9+
#include <sstream>
10+
#include <fstream>
11+
#include "libswirl/hw/aica/dsp_backend.h"
12+
13+
using namespace std;
14+
15+
map<string, int> getDefaultInst()
16+
{
17+
map<string, int> rv;
18+
19+
rv["TRA"] = 0;
20+
rv["TWT"] = 0;
21+
rv["TWA"] = 0;
22+
rv["XSEL"] = 0;
23+
rv["YSEL"] = 0;
24+
rv["IRA"] = 0;
25+
rv["IWT"] = 0;
26+
rv["IWA"] = 0;
27+
rv["EWT"] = 0;
28+
rv["EWA"] = 0;
29+
rv["ADRL"] = 0;
30+
rv["FRCL"] = 0;
31+
rv["SHIFT"] = 0;
32+
rv["YRL"] = 0;
33+
rv["NEGB"] = 0;
34+
rv["ZERO"] = 0;
35+
rv["BSEL"] = 0;
36+
rv["NOFL"] = 0;
37+
rv["TABLE"] = 0;
38+
rv["MWT"] = 0;
39+
rv["MRD"] = 0;
40+
rv["MASA"] = 0;
41+
rv["ADREB"] = 0;
42+
rv["NXADR"] = 0;
43+
44+
return rv;
45+
}
46+
47+
map<string, int> decodeInst(u32 *Inst)
48+
{
49+
_INST i = {0};
50+
51+
DSPBackend::DecodeInst(Inst, &i);
52+
53+
map<string, int> rv;
54+
55+
rv["TRA"] = i.TRA;
56+
rv["TWT"] = i.TWT;
57+
rv["TWA"] = i.TWA;
58+
rv["XSEL"] = i.XSEL;
59+
rv["YSEL"] = i.YSEL;
60+
rv["IRA"] = i.IRA;
61+
rv["IWT"] = i.IWT;
62+
rv["IWA"] = i.IWA;
63+
rv["EWT"] = i.EWT;
64+
rv["EWA"] = i.EWA;
65+
rv["ADRL"] = i.ADRL;
66+
rv["FRCL"] = i.FRCL;
67+
rv["SHIFT"] = i.SHIFT;
68+
rv["YRL"] = i.YRL;
69+
rv["NEGB"] = i.NEGB;
70+
rv["ZERO"] = i.ZERO;
71+
rv["BSEL"] = i.BSEL;
72+
rv["NOFL"] = i.NOFL;
73+
rv["TABLE"] = i.TABLE;
74+
rv["MWT"] = i.MWT;
75+
rv["MRD"] = i.MRD;
76+
rv["MASA"] = i.MASA;
77+
rv["ADREB"] = i.ADREB;
78+
rv["NXADR"] = i.NXADR;
79+
80+
return rv;
81+
}
82+
83+
void encodeInst(u32 *Inst, map<string, int> &desc)
84+
{
85+
_INST i = {0};
86+
87+
i.TRA = desc["TRA"];
88+
i.TWT = desc["TWT"];
89+
i.TWA = desc["TWA"];
90+
i.XSEL = desc["XSEL"];
91+
i.YSEL = desc["YSEL"];
92+
i.IRA = desc["IRA"];
93+
i.IWT = desc["IWT"];
94+
i.IWA = desc["IWA"];
95+
i.EWT = desc["EWT"];
96+
i.EWA = desc["EWA"];
97+
i.ADRL = desc["ADRL"];
98+
i.FRCL = desc["FRCL"];
99+
i.SHIFT = desc["SHIFT"];
100+
i.YRL = desc["YRL"];
101+
i.NEGB = desc["NEGB"];
102+
i.ZERO = desc["ZERO"];
103+
i.BSEL = desc["BSEL"];
104+
i.NOFL = desc["NOFL"];
105+
i.TABLE = desc["TABLE"];
106+
i.MWT = desc["MWT"];
107+
i.MRD = desc["MRD"];
108+
i.MASA = desc["MASA"];
109+
i.ADREB = desc["ADREB"];
110+
i.NXADR = desc["NXADR"];
111+
112+
DSPBackend::EncodeInst(Inst, &i);
113+
}
114+
115+
string DisassembleDesc(map<string, int> &desc)
116+
{
117+
stringstream rv;
118+
119+
rv << "TRA:" << desc["TRA"] << " ";
120+
rv << "TWT:" << desc["TWT"] << " ";
121+
rv << "TWA:" << desc["TWA"] << " ";
122+
rv << "XSEL:" << desc["XSEL"] << " ";
123+
rv << "YSEL:" << desc["YSEL"] << " ";
124+
rv << "IRA:" << desc["IRA"] << " ";
125+
rv << "IWT:" << desc["IWT"] << " ";
126+
rv << "IWA:" << desc["IWA"] << " ";
127+
rv << "EWT:" << desc["EWT"] << " ";
128+
rv << "EWA:" << desc["EWA"] << " ";
129+
rv << "ADRL:" << desc["ADRL"] << " ";
130+
rv << "FRCL:" << desc["FRCL"] << " ";
131+
rv << "SHIFT:" << desc["SHIFT"] << " ";
132+
rv << "YRL:" << desc["YRL"] << " ";
133+
rv << "NEGB:" << desc["NEGB"] << " ";
134+
rv << "ZERO:" << desc["ZERO"] << " ";
135+
rv << "BSEL:" << desc["BSEL"] << " ";
136+
rv << "NOFL:" << desc["NOFL"] << " ";
137+
rv << "TABLE:" << desc["TABLE"] << " ";
138+
rv << "MWT:" << desc["MWT"] << " ";
139+
rv << "MRD:" << desc["MRD"] << " ";
140+
rv << "MASA:" << desc["MASA"] << " ";
141+
rv << "ADREB:" << desc["ADREB"] << " ";
142+
rv << "NXADR:" << desc["NXADR"];
143+
144+
return rv.str();
145+
}
146+
147+
map<string, int> AssembleDesc(string text)
148+
{
149+
map<string, int> rv = getDefaultInst();
150+
istringstream line(text);
151+
152+
string word;
153+
while (line >> word)
154+
{
155+
istringstream wordstream(word);
156+
string decl, value;
157+
if (!getline(wordstream, decl, ':'))
158+
{
159+
printf("Invalid asm %s\n", text.c_str());
160+
exit(-4);
161+
}
162+
if (!(wordstream >> value))
163+
{
164+
printf("Invalid asm %s\n", text.c_str());
165+
exit(-5);
166+
}
167+
168+
rv[decl] = atoi(value.c_str());
169+
}
170+
171+
return rv;
172+
}
173+
174+
int main(int argc, char **argv)
175+
{
176+
177+
if (argc != 2 && argc != 3)
178+
{
179+
printf("expected %s src_file [-d]\n", argv[0]);
180+
return -1;
181+
}
182+
183+
uint32_t mpro[128 * 4] = {0};
184+
185+
if (argc == 2)
186+
{
187+
const string inputFile = argv[1];
188+
189+
ifstream infile(inputFile, std::ios_base::binary);
190+
191+
std::string line;
192+
int op = 0;
193+
while (getline(infile, line))
194+
{
195+
if (line.size() == 0 || line[0] == '#')
196+
continue;
197+
if (op == 128)
198+
{
199+
printf("more than 128 ops\n");
200+
exit(-5);
201+
}
202+
203+
auto desc = AssembleDesc(line);
204+
encodeInst(&mpro[op * 4], desc);
205+
op++;
206+
}
207+
208+
freopen(NULL, "wb", stdout);
209+
fwrite(mpro, 4, 128 * 4, stdout);
210+
}
211+
else
212+
{
213+
FILE *f = fopen(argv[1], "rb");
214+
if (!f)
215+
{
216+
printf("failed to open %s\n", argv[1]);
217+
return -2;
218+
}
219+
if (fread(mpro, 4, 128 * 4, f) != 128 * 4)
220+
{
221+
printf("file %s is not the right size\n", argv[1]);
222+
return -3;
223+
}
224+
fclose(f);
225+
226+
for (int i = 0; i < 128; i++)
227+
{
228+
auto inst = decodeInst(&mpro[i * 4]);
229+
230+
auto desc = decodeInst(&mpro[i * 4]);
231+
auto text = DisassembleDesc(desc);
232+
printf("#step %d\n", i);
233+
puts(text.c_str());
234+
}
235+
}
236+
}

libswirl/hw/aica/dsp.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,87 +13,6 @@
1313

1414
DECL_ALIGN(4096) dsp_context_t dsp;
1515

16-
//float format is ?
17-
u16 DYNACALL DSPBackend::PACK(s32 val)
18-
{
19-
u32 temp;
20-
int sign, exponent, k;
21-
22-
sign = (val >> 23) & 0x1;
23-
temp = (val ^ (val << 1)) & 0xFFFFFF;
24-
exponent = 0;
25-
for (k = 0; k < 12; k++)
26-
{
27-
if (temp & 0x800000)
28-
break;
29-
temp <<= 1;
30-
exponent += 1;
31-
}
32-
if (exponent < 12)
33-
val = (val << exponent) & 0x3FFFFF;
34-
else
35-
val <<= 11;
36-
val >>= 11;
37-
val |= sign << 15;
38-
val |= exponent << 11;
39-
40-
return (u16)val;
41-
}
42-
43-
s32 DYNACALL DSPBackend::UNPACK(u16 val)
44-
{
45-
int sign, exponent, mantissa;
46-
s32 uval;
47-
48-
sign = (val >> 15) & 0x1;
49-
exponent = (val >> 11) & 0xF;
50-
mantissa = val & 0x7FF;
51-
uval = mantissa << 11;
52-
if (exponent > 11)
53-
exponent = 11;
54-
else
55-
uval |= (sign ^ 1) << 22;
56-
uval |= sign << 23;
57-
uval <<= 8;
58-
uval >>= 8;
59-
uval >>= exponent;
60-
61-
return uval;
62-
}
63-
64-
void DSPBackend::DecodeInst(u32* IPtr, _INST* i)
65-
{
66-
i->TRA = (IPtr[0] >> 9) & 0x7F;
67-
i->TWT = (IPtr[0] >> 8) & 0x01;
68-
i->TWA = (IPtr[0] >> 1) & 0x7F;
69-
70-
i->XSEL = (IPtr[1] >> 15) & 0x01;
71-
i->YSEL = (IPtr[1] >> 13) & 0x03;
72-
i->IRA = (IPtr[1] >> 7) & 0x3F;
73-
i->IWT = (IPtr[1] >> 6) & 0x01;
74-
i->IWA = (IPtr[1] >> 1) & 0x1F;
75-
76-
i->TABLE = (IPtr[2] >> 15) & 0x01;
77-
i->MWT = (IPtr[2] >> 14) & 0x01;
78-
i->MRD = (IPtr[2] >> 13) & 0x01;
79-
i->EWT = (IPtr[2] >> 12) & 0x01;
80-
i->EWA = (IPtr[2] >> 8) & 0x0F;
81-
i->ADRL = (IPtr[2] >> 7) & 0x01;
82-
i->FRCL = (IPtr[2] >> 6) & 0x01;
83-
i->SHIFT = (IPtr[2] >> 4) & 0x03;
84-
i->YRL = (IPtr[2] >> 3) & 0x01;
85-
i->NEGB = (IPtr[2] >> 2) & 0x01;
86-
i->ZERO = (IPtr[2] >> 1) & 0x01;
87-
i->BSEL = (IPtr[2] >> 0) & 0x01;
88-
89-
i->NOFL = (IPtr[3] >> 15) & 1; //????
90-
//i->COEF=(IPtr[3]>>9)&0x3f;
91-
92-
i->MASA = (IPtr[3] >> 9) & 0x3f; //???
93-
i->ADREB = (IPtr[3] >> 8) & 0x1;
94-
i->NXADR = (IPtr[3] >> 7) & 0x1;
95-
}
96-
9716
struct DSP_impl final : DSP {
9817
u8* aica_ram;
9918
u32 aram_size;

libswirl/hw/aica/dsp_backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ struct DSPBackend {
128128
static u16 DYNACALL PACK(s32 val);
129129
static s32 DYNACALL UNPACK(u16 val);
130130
static void DecodeInst(u32* IPtr, _INST* i);
131+
static void EncodeInst(u32* IPtr, _INST* i);
131132

132133
virtual void Step() = 0;
133134
virtual void Recompile() = 0;

0 commit comments

Comments
 (0)