Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1835 from reicast/skmp/aica-dsp-asm
Browse files Browse the repository at this point in the history
DSP: Add assembler/disassembler
  • Loading branch information
skmp committed Mar 20, 2020
2 parents ad7452a + b67a530 commit 3f07c32
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 81 deletions.
1 change: 1 addition & 0 deletions aica-dsp-asm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g++ main.cpp ../libswirl/hw/aica/dsp_helpers.cpp -I.. -I../libswirl -DTARGET_LINUX_x64 -o aica-dsp-asm
236 changes: 236 additions & 0 deletions aica-dsp-asm/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
This is part of libswirl
*/
#include <license/bsd>

#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <fstream>
#include "libswirl/hw/aica/dsp_backend.h"

using namespace std;

map<string, int> getDefaultInst()
{
map<string, int> rv;

rv["TRA"] = 0;
rv["TWT"] = 0;
rv["TWA"] = 0;
rv["XSEL"] = 0;
rv["YSEL"] = 0;
rv["IRA"] = 0;
rv["IWT"] = 0;
rv["IWA"] = 0;
rv["EWT"] = 0;
rv["EWA"] = 0;
rv["ADRL"] = 0;
rv["FRCL"] = 0;
rv["SHIFT"] = 0;
rv["YRL"] = 0;
rv["NEGB"] = 0;
rv["ZERO"] = 0;
rv["BSEL"] = 0;
rv["NOFL"] = 0;
rv["TABLE"] = 0;
rv["MWT"] = 0;
rv["MRD"] = 0;
rv["MASA"] = 0;
rv["ADREB"] = 0;
rv["NXADR"] = 0;

return rv;
}

map<string, int> decodeInst(u32 *Inst)
{
_INST i = {0};

DSPBackend::DecodeInst(Inst, &i);

map<string, int> rv;

rv["TRA"] = i.TRA;
rv["TWT"] = i.TWT;
rv["TWA"] = i.TWA;
rv["XSEL"] = i.XSEL;
rv["YSEL"] = i.YSEL;
rv["IRA"] = i.IRA;
rv["IWT"] = i.IWT;
rv["IWA"] = i.IWA;
rv["EWT"] = i.EWT;
rv["EWA"] = i.EWA;
rv["ADRL"] = i.ADRL;
rv["FRCL"] = i.FRCL;
rv["SHIFT"] = i.SHIFT;
rv["YRL"] = i.YRL;
rv["NEGB"] = i.NEGB;
rv["ZERO"] = i.ZERO;
rv["BSEL"] = i.BSEL;
rv["NOFL"] = i.NOFL;
rv["TABLE"] = i.TABLE;
rv["MWT"] = i.MWT;
rv["MRD"] = i.MRD;
rv["MASA"] = i.MASA;
rv["ADREB"] = i.ADREB;
rv["NXADR"] = i.NXADR;

return rv;
}

void encodeInst(u32 *Inst, map<string, int> &desc)
{
_INST i = {0};

i.TRA = desc["TRA"];
i.TWT = desc["TWT"];
i.TWA = desc["TWA"];
i.XSEL = desc["XSEL"];
i.YSEL = desc["YSEL"];
i.IRA = desc["IRA"];
i.IWT = desc["IWT"];
i.IWA = desc["IWA"];
i.EWT = desc["EWT"];
i.EWA = desc["EWA"];
i.ADRL = desc["ADRL"];
i.FRCL = desc["FRCL"];
i.SHIFT = desc["SHIFT"];
i.YRL = desc["YRL"];
i.NEGB = desc["NEGB"];
i.ZERO = desc["ZERO"];
i.BSEL = desc["BSEL"];
i.NOFL = desc["NOFL"];
i.TABLE = desc["TABLE"];
i.MWT = desc["MWT"];
i.MRD = desc["MRD"];
i.MASA = desc["MASA"];
i.ADREB = desc["ADREB"];
i.NXADR = desc["NXADR"];

DSPBackend::EncodeInst(Inst, &i);
}

string DisassembleDesc(map<string, int> &desc)
{
stringstream rv;

rv << "TRA:" << desc["TRA"] << " ";
rv << "TWT:" << desc["TWT"] << " ";
rv << "TWA:" << desc["TWA"] << " ";
rv << "XSEL:" << desc["XSEL"] << " ";
rv << "YSEL:" << desc["YSEL"] << " ";
rv << "IRA:" << desc["IRA"] << " ";
rv << "IWT:" << desc["IWT"] << " ";
rv << "IWA:" << desc["IWA"] << " ";
rv << "EWT:" << desc["EWT"] << " ";
rv << "EWA:" << desc["EWA"] << " ";
rv << "ADRL:" << desc["ADRL"] << " ";
rv << "FRCL:" << desc["FRCL"] << " ";
rv << "SHIFT:" << desc["SHIFT"] << " ";
rv << "YRL:" << desc["YRL"] << " ";
rv << "NEGB:" << desc["NEGB"] << " ";
rv << "ZERO:" << desc["ZERO"] << " ";
rv << "BSEL:" << desc["BSEL"] << " ";
rv << "NOFL:" << desc["NOFL"] << " ";
rv << "TABLE:" << desc["TABLE"] << " ";
rv << "MWT:" << desc["MWT"] << " ";
rv << "MRD:" << desc["MRD"] << " ";
rv << "MASA:" << desc["MASA"] << " ";
rv << "ADREB:" << desc["ADREB"] << " ";
rv << "NXADR:" << desc["NXADR"];

return rv.str();
}

map<string, int> AssembleDesc(string text)
{
map<string, int> rv = getDefaultInst();
istringstream line(text);

string word;
while (line >> word)
{
istringstream wordstream(word);
string decl, value;
if (!getline(wordstream, decl, ':'))
{
printf("Invalid asm %s\n", text.c_str());
exit(-4);
}
if (!(wordstream >> value))
{
printf("Invalid asm %s\n", text.c_str());
exit(-5);
}

rv[decl] = atoi(value.c_str());
}

return rv;
}

int main(int argc, char **argv)
{

if (argc != 2 && argc != 3)
{
printf("expected %s src_file [-d]\n", argv[0]);
return -1;
}

uint32_t mpro[128 * 4] = {0};

if (argc == 2)
{
const string inputFile = argv[1];

ifstream infile(inputFile, std::ios_base::binary);

std::string line;
int op = 0;
while (getline(infile, line))
{
if (line.size() == 0 || line[0] == '#')
continue;
if (op == 128)
{
printf("more than 128 ops\n");
exit(-5);
}

auto desc = AssembleDesc(line);
encodeInst(&mpro[op * 4], desc);
op++;
}

freopen(NULL, "wb", stdout);
fwrite(mpro, 4, 128 * 4, stdout);
}
else
{
FILE *f = fopen(argv[1], "rb");
if (!f)
{
printf("failed to open %s\n", argv[1]);
return -2;
}
if (fread(mpro, 4, 128 * 4, f) != 128 * 4)
{
printf("file %s is not the right size\n", argv[1]);
return -3;
}
fclose(f);

for (int i = 0; i < 128; i++)
{
auto inst = decodeInst(&mpro[i * 4]);

auto desc = decodeInst(&mpro[i * 4]);
auto text = DisassembleDesc(desc);
printf("#step %d\n", i);
puts(text.c_str());
}
}
}
81 changes: 0 additions & 81 deletions libswirl/hw/aica/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,87 +13,6 @@

DECL_ALIGN(4096) dsp_context_t dsp;

//float format is ?
u16 DYNACALL DSPBackend::PACK(s32 val)
{
u32 temp;
int sign, exponent, k;

sign = (val >> 23) & 0x1;
temp = (val ^ (val << 1)) & 0xFFFFFF;
exponent = 0;
for (k = 0; k < 12; k++)
{
if (temp & 0x800000)
break;
temp <<= 1;
exponent += 1;
}
if (exponent < 12)
val = (val << exponent) & 0x3FFFFF;
else
val <<= 11;
val >>= 11;
val |= sign << 15;
val |= exponent << 11;

return (u16)val;
}

s32 DYNACALL DSPBackend::UNPACK(u16 val)
{
int sign, exponent, mantissa;
s32 uval;

sign = (val >> 15) & 0x1;
exponent = (val >> 11) & 0xF;
mantissa = val & 0x7FF;
uval = mantissa << 11;
if (exponent > 11)
exponent = 11;
else
uval |= (sign ^ 1) << 22;
uval |= sign << 23;
uval <<= 8;
uval >>= 8;
uval >>= exponent;

return uval;
}

void DSPBackend::DecodeInst(u32* IPtr, _INST* i)
{
i->TRA = (IPtr[0] >> 9) & 0x7F;
i->TWT = (IPtr[0] >> 8) & 0x01;
i->TWA = (IPtr[0] >> 1) & 0x7F;

i->XSEL = (IPtr[1] >> 15) & 0x01;
i->YSEL = (IPtr[1] >> 13) & 0x03;
i->IRA = (IPtr[1] >> 7) & 0x3F;
i->IWT = (IPtr[1] >> 6) & 0x01;
i->IWA = (IPtr[1] >> 1) & 0x1F;

i->TABLE = (IPtr[2] >> 15) & 0x01;
i->MWT = (IPtr[2] >> 14) & 0x01;
i->MRD = (IPtr[2] >> 13) & 0x01;
i->EWT = (IPtr[2] >> 12) & 0x01;
i->EWA = (IPtr[2] >> 8) & 0x0F;
i->ADRL = (IPtr[2] >> 7) & 0x01;
i->FRCL = (IPtr[2] >> 6) & 0x01;
i->SHIFT = (IPtr[2] >> 4) & 0x03;
i->YRL = (IPtr[2] >> 3) & 0x01;
i->NEGB = (IPtr[2] >> 2) & 0x01;
i->ZERO = (IPtr[2] >> 1) & 0x01;
i->BSEL = (IPtr[2] >> 0) & 0x01;

i->NOFL = (IPtr[3] >> 15) & 1; //????
//i->COEF=(IPtr[3]>>9)&0x3f;

i->MASA = (IPtr[3] >> 9) & 0x3f; //???
i->ADREB = (IPtr[3] >> 8) & 0x1;
i->NXADR = (IPtr[3] >> 7) & 0x1;
}

struct DSP_impl final : DSP {
u8* aica_ram;
u32 aram_size;
Expand Down
1 change: 1 addition & 0 deletions libswirl/hw/aica/dsp_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct DSPBackend {
static u16 DYNACALL PACK(s32 val);
static s32 DYNACALL UNPACK(u16 val);
static void DecodeInst(u32* IPtr, _INST* i);
static void EncodeInst(u32* IPtr, _INST* i);

virtual void Step() = 0;
virtual void Recompile() = 0;
Expand Down
Loading

0 comments on commit 3f07c32

Please sign in to comment.