mirror of
https://gitlab.com/jusax23/raspberry-pi-pico-machine-code-emulator.git
synced 2024-11-22 22:56:36 +01:00
844 lines
24 KiB
C++
844 lines
24 KiB
C++
#define pram_length 65536
|
|
#define program_length 65536
|
|
|
|
//#define s_debug
|
|
|
|
byte pram[pram_length];
|
|
byte prog[program_length];
|
|
|
|
uint16_t pcounter = 0;
|
|
uint16_t pstackpointer = pram_length-1;
|
|
uint16_t pframepointer = pram_length-1;
|
|
uint16_t pstackmax = pram_length*0.5;
|
|
|
|
uint32_t AReg;
|
|
uint32_t BReg;
|
|
uint32_t CReg;
|
|
|
|
uint32_t XReg;
|
|
|
|
bool equalFlag;
|
|
bool biggerFlag;
|
|
bool smalerFlag;
|
|
|
|
bool doExecute = false;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
pinMode(25, OUTPUT);
|
|
}
|
|
|
|
String b16 = "0123456789abcdef";
|
|
|
|
|
|
void loop() {
|
|
while (true){
|
|
while(Serial.available()){
|
|
String c = Serial.readStringUntil('\n');
|
|
if (c == "start")doExecute = true;
|
|
else if (c == "stop")doExecute = false;
|
|
else if (c == "test"){
|
|
for(int i = 0; i < 115; i++) {
|
|
Serial.println(prog[i]);
|
|
}
|
|
Serial.println(pcounter);
|
|
}
|
|
else if (c == "clear"){
|
|
pcounter = 0;
|
|
pstackpointer = pram_length-1;
|
|
pstackmax = pram_length*0.5;
|
|
AReg = 0;
|
|
BReg = 0;
|
|
CReg = 0;
|
|
XReg = 0;
|
|
equalFlag = false;
|
|
biggerFlag = false;
|
|
smalerFlag = false;
|
|
for (int i = 0; i < pram_length; i++) {
|
|
pram[i] = 0;
|
|
}
|
|
}
|
|
else{
|
|
for (int i = 0; i < c.length()-1; i=i+2) {
|
|
prog[i/2] = b16.indexOf(c[i])*16+b16.indexOf(c[i+1]);
|
|
}
|
|
}
|
|
}
|
|
if(doExecute){
|
|
digitalWrite(25, true);
|
|
#ifdef s_debug
|
|
Serial.print("Line: ");
|
|
Serial.print(pcounter);
|
|
Serial.print(" > ");
|
|
Serial.print(prog[pcounter]);
|
|
Serial.print(" > ");
|
|
Serial.print(pram[65528]);
|
|
Serial.print(" > ");
|
|
Serial.print(pstackpointer);
|
|
Serial.println(" ... ");
|
|
#endif
|
|
execute();
|
|
#ifdef s_debug
|
|
Serial.print(" ... ");
|
|
Serial.print(pram[65528]);
|
|
Serial.print(" > ");
|
|
Serial.println("done");
|
|
#endif
|
|
}else{
|
|
digitalWrite(25, false);
|
|
}
|
|
}
|
|
|
|
|
|
//delay(1);
|
|
}
|
|
|
|
void execute(){
|
|
byte cmd = prog[pcounter++];
|
|
switch ((int)cmd){
|
|
case 0:{
|
|
__asm__("nop");
|
|
}break;
|
|
case 1:{ // LDA
|
|
byte s = prog[pcounter++];
|
|
byte n = min(s&0xf,4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
unsigned short addr = ((s&0xf0)!=0)?max(pframepointer+1-data-n,pstackpointer):data;
|
|
/*Serial.print("LDA");
|
|
Serial.print(s);
|
|
Serial.print(">");
|
|
Serial.print(s&0xf0);
|
|
Serial.print(">");
|
|
Serial.print((s&0xf0)!=0);
|
|
Serial.print(">");
|
|
Serial.print(max(pframepointer+1-data-n,pstackpointer));
|
|
Serial.print(">");
|
|
Serial.print(pframepointer-data-n);
|
|
Serial.print(">");
|
|
Serial.println(addr);*/
|
|
AReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
AReg += pram[addr+i]<<(i*8);
|
|
}
|
|
}break;
|
|
case 2:{ // LDB
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
BReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
BReg += pram[data+i]<<(i*8);
|
|
}
|
|
}break;
|
|
case 3:{ // LDC
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
CReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
CReg += pram[data+i]<<(i*8);
|
|
}
|
|
}break;
|
|
case 4:{ // LDX
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
XReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
XReg += pram[data+i]<<(i*8);
|
|
}
|
|
}break;
|
|
|
|
case 5:{ // LOA
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
data += XReg*n;
|
|
AReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
AReg += pram[data+i]<<(i*8);
|
|
}
|
|
}break;
|
|
case 6:{ // LOB
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
data += XReg*n;
|
|
BReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
BReg += pram[data+i]<<(i*8);
|
|
}
|
|
}break;
|
|
case 7:{ // LOC
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
data += XReg*n;
|
|
CReg = 0;
|
|
for (byte i = 0; i < n; i++) {
|
|
CReg += pram[data+i]<<(i*8);
|
|
}
|
|
}break;
|
|
|
|
case 8:{ // STA
|
|
byte s = prog[pcounter++];
|
|
byte n = min(s&0xf,4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
unsigned short addr = ((s&0xf0)!=0)?max(pframepointer+1-data-n,pstackpointer):data;
|
|
/*Serial.print("STA");
|
|
Serial.print(s);
|
|
Serial.print(">");
|
|
Serial.print(s&0xf0);
|
|
Serial.print(">");
|
|
Serial.print((s&0xf0)!=0);
|
|
Serial.print(">");
|
|
Serial.print(max(pframepointer+1-data-n,pstackpointer));
|
|
Serial.print(">");
|
|
Serial.println(addr);*/
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[addr+i] = (AReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
case 9:{ // STB
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[data+i] = (BReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
case 10:{ // STC
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[data+i] = (CReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
case 11:{ // STX
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[data+i] = (XReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
|
|
case 12:{ // SOA
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
data += XReg*n;
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[data+i] = (AReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
case 13:{ // SOB
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
data += XReg*n;
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[data+i] = (BReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
case 14:{ // SOC
|
|
byte n = min(prog[pcounter++],4);
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
data += XReg*n;
|
|
for (byte i = 0; i < n; i++) {
|
|
pram[data+i] = (CReg>>(i*8))&0xff;
|
|
}
|
|
}break;
|
|
|
|
case 15:{ // LIA
|
|
uint32_t data = prog[pcounter++]<<24;
|
|
data += prog[pcounter++]<<16;
|
|
data += prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
AReg = data;
|
|
}break;
|
|
case 16:{ // LIB
|
|
uint32_t data = prog[pcounter++]<<24;
|
|
data += prog[pcounter++]<<16;
|
|
data += prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
BReg = data;
|
|
}break;
|
|
case 17:{ // LIC
|
|
uint32_t data = prog[pcounter++]<<24;
|
|
data += prog[pcounter++]<<16;
|
|
data += prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
CReg = data;
|
|
}break;
|
|
case 18:{ // LIX
|
|
uint32_t data = prog[pcounter++]<<24;
|
|
data += prog[pcounter++]<<16;
|
|
data += prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
XReg = data;
|
|
}break;
|
|
|
|
case 19:{ // CAB
|
|
BReg = AReg;
|
|
}break;
|
|
case 20:{ // CBC
|
|
CReg = BReg;
|
|
}break;
|
|
case 21:{ // CCA
|
|
AReg = CReg;
|
|
}break;
|
|
case 22:{ // CAC
|
|
CReg = AReg;
|
|
}break;
|
|
case 23:{ // CCB
|
|
BReg = CReg;
|
|
}break;
|
|
case 24:{ // CBA
|
|
AReg = BReg;
|
|
}break;
|
|
|
|
case 25:{ // CAX
|
|
XReg = AReg;
|
|
}break;
|
|
case 26:{ // CBX
|
|
XReg = BReg;
|
|
}break;
|
|
case 27:{ // CCX
|
|
XReg = CReg;
|
|
}break;
|
|
case 28:{ // CXA
|
|
AReg = XReg;
|
|
}break;
|
|
case 29:{ // CXB
|
|
BReg = XReg;
|
|
}break;
|
|
case 30:{ // CXC
|
|
CReg = XReg;
|
|
}break;
|
|
|
|
//Types unsigned 0: uint;
|
|
// signed 1: int; 2: float
|
|
|
|
case 31: { //CVA
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
if(data2 == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 2){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data2 == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 32:{ //CVB
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
if(data2 == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &BReg;}
|
|
else if(data1 == 0){o=BReg;}
|
|
else if(data1 == 2){o= * ( float * ) &BReg;}
|
|
BReg = * (uint32_t * ) &o;
|
|
}else if(data2 == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &BReg;}
|
|
else if(data1 == 0){o=BReg;}
|
|
else if(data1 == 2){o= * ( float * ) &BReg;}
|
|
BReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &BReg;}
|
|
else if(data1 == 0){o=BReg;}
|
|
else if(data1 == 2){o= * ( float * ) &BReg;}
|
|
BReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 33:{ //CVC
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
if(data2 == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &CReg;}
|
|
else if(data1 == 0){o=CReg;}
|
|
else if(data1 == 2){o= * ( float * ) &CReg;}
|
|
CReg = * (uint32_t * ) &o;
|
|
}else if(data2 == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &CReg;}
|
|
else if(data1 == 0){o=CReg;}
|
|
else if(data1 == 2){o= * ( float * ) &CReg;}
|
|
CReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &CReg;}
|
|
else if(data1 == 0){o=CReg;}
|
|
else if(data1 == 2){o= * ( float * ) &CReg;}
|
|
CReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
|
|
|
|
case 34:{ // ADD
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o+= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o+=BReg;}
|
|
else if(data2 == 2){o+= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o+= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o+=BReg;}
|
|
else if(data2 == 2){o+= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o+= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o+=BReg;}
|
|
else if(data2 == 2){o+= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 35:{ // SUB
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o-= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o-=BReg;}
|
|
else if(data2 == 2){o-= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o-= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o-=BReg;}
|
|
else if(data2 == 2){o-= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o-= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o-=BReg;}
|
|
else if(data2 == 2){o-= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 36:{ //MUL
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o*= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o*=BReg;}
|
|
else if(data2 == 2){o*= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o*= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o*=BReg;}
|
|
else if(data2 == 2){o*= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o*= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o*=BReg;}
|
|
else if(data2 == 2){o*= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 37:{ //DIV
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o/= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o/=BReg;}
|
|
else if(data2 == 2){o/= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o/= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o/=BReg;}
|
|
else if(data2 == 2){o/= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else{
|
|
uint32_t o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o/= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o/=BReg;}
|
|
else if(data2 == 2){o/= * ( float * ) &BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 38:{ //MOD
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
uint32_t o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
if(data2 == 1){o = o % (* ( int32_t * ) &BReg);}
|
|
else if(data2 == 0){o=o%BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else{
|
|
int32_t o = 1;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
if(data2 == 1){o = o % (* ( int32_t * ) &BReg);}
|
|
else if(data2 == 0){o=o%BReg;}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
|
|
case 39:{ // POW
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o=pow(o, * ( int32_t * ) &BReg);}
|
|
else if(data2 == 0){o=pow(o, BReg);}
|
|
else if(data2 == 2){o=pow(o, * ( float * ) &BReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o=pow(o, * ( int32_t * ) &BReg);}
|
|
else if(data2 == 0){o=pow(o, BReg);}
|
|
else if(data2 == 2){o=pow(o, * ( float * ) &BReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o=pow(o, * ( int32_t * ) &BReg);}
|
|
else if(data2 == 0){o=pow(o, BReg);}
|
|
else if(data2 == 2){o=pow(o, * ( float * ) &BReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 40:{ // ROT
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o=pow(o,1.0/ (* ( int32_t * ) &BReg));}
|
|
else if(data2 == 0){o=pow(o, 1.0/BReg);}
|
|
else if(data2 == 2){o=pow(o,1.0/( * ( float * ) &BReg));}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o=pow(o,1.0/ (* ( int32_t * ) &BReg));}
|
|
else if(data2 == 0){o=pow(o, 1.0/BReg);}
|
|
else if(data2 == 2){o=pow(o,1.0/( * ( float * ) &BReg));}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o=pow(o, * ( int32_t * ) &BReg);}
|
|
else if(data2 == 0){o=pow(o, BReg);}
|
|
else if(data2 == 2){o=pow(o, * ( float * ) &BReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
case 41:{ // LOG
|
|
byte data = prog[pcounter++];
|
|
byte data1 = prog[pcounter++];
|
|
|
|
if(data == 2){
|
|
float o = 0;
|
|
if(data1 == 1){o=log( * ( int32_t * ) &AReg);}
|
|
else if(data1 == 0){o=log(AReg);}
|
|
else if(data1 == 2){o= log(* ( float * ) &AReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else if(data == 1){
|
|
int32_t o = 0;
|
|
if(data1 == 1){o=log( * ( int32_t * ) &AReg);}
|
|
else if(data1 == 0){o=log(AReg);}
|
|
else if(data1 == 2){o= log(* ( float * ) &AReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}else {
|
|
uint32_t o = 0;
|
|
if(data1 == 1){o=log( * ( int32_t * ) &AReg);}
|
|
else if(data1 == 0){o=log(AReg);}
|
|
else if(data1 == 2){o= log(* ( float * ) &AReg);}
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
|
|
case 42:{ // SR
|
|
AReg = AReg >> BReg;
|
|
}break;
|
|
case 43:{ // SL
|
|
AReg = AReg << BReg;
|
|
}break;
|
|
|
|
case 44:{ // INC
|
|
byte data1 = prog[pcounter++];
|
|
if(data1==0||data1==1){
|
|
AReg++;
|
|
}else if(data1==2){
|
|
float o = * (float *) &AReg;
|
|
o++;
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
|
|
}break;
|
|
case 45:{ // DEC
|
|
byte data1 = prog[pcounter++];
|
|
if(data1==0||data1==1){
|
|
AReg--;
|
|
}else if(data1==2){
|
|
float o = * (float *) &AReg;
|
|
o--;
|
|
AReg = * (uint32_t * ) &o;
|
|
}
|
|
}break;
|
|
|
|
case 46:{ // BWO
|
|
AReg = AReg | BReg;
|
|
}break;
|
|
case 47:{ // BWA
|
|
AReg = AReg & BReg;
|
|
}break;
|
|
case 48:{ // BWX
|
|
AReg = AReg ^ BReg;
|
|
}break;
|
|
|
|
case 49:{ //CMP
|
|
byte data1 = prog[pcounter++];
|
|
byte data2 = prog[pcounter++];
|
|
|
|
double o = 0;
|
|
if(data1 == 1){o= * ( int32_t * ) &AReg;}
|
|
else if(data1 == 0){o=AReg;}
|
|
else if(data1 == 2){o= * ( float * ) &AReg;}
|
|
if(data2 == 1){o-= * ( int32_t * ) &BReg;}
|
|
else if(data2 == 0){o-=BReg;}
|
|
else if(data2 == 2){o-= * ( float * ) &BReg;}
|
|
equalFlag = (fabs(o) < 0.00005f);
|
|
biggerFlag = (o >= 0.00005f);
|
|
smalerFlag = (o <= -0.00005f);
|
|
}break;
|
|
case 50:{ //JMP
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
pcounter = data;
|
|
}break;
|
|
case 51:{ //JE
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
if(equalFlag){
|
|
pcounter = data;
|
|
}
|
|
}break;
|
|
case 52:{ //JNE
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
if(!equalFlag){
|
|
pcounter = data;
|
|
}
|
|
}break;
|
|
case 53:{ //JB
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
if(biggerFlag){
|
|
pcounter = data;
|
|
}
|
|
}break;
|
|
case 54:{ //JNB
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
if(!biggerFlag){
|
|
pcounter = data;
|
|
}
|
|
}break;
|
|
case 55:{ //JS
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
if(smalerFlag){
|
|
pcounter = data;
|
|
}
|
|
}break;
|
|
case 56:{ //JNS
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
if(!smalerFlag){
|
|
pcounter = data;
|
|
}
|
|
}break;
|
|
|
|
case 57:{ //PSH
|
|
int data = max(min(prog[pcounter++],255),1);
|
|
for (int i = data-1; i >= 0; i--) {
|
|
if(i<=4){
|
|
pram[pstackpointer--] = (AReg >> (i*8))&0xff;
|
|
}else{
|
|
pram[pstackpointer--] = 0;
|
|
}
|
|
}
|
|
if(pram_length-pstackpointer>pstackmax){
|
|
doExecute = false;
|
|
Serial.println("Stackoverflow: PSH");
|
|
}
|
|
}break;
|
|
case 58:{ //PUL
|
|
int data = max(min(prog[pcounter++]+0,255),1);
|
|
AReg = 0;
|
|
for (int i = 0; i < data; i++) {
|
|
if(i<=4){
|
|
AReg+=pram[++pstackpointer]<<(i*8);
|
|
}else{
|
|
pstackpointer++;
|
|
}
|
|
}
|
|
if(pram_length-pstackpointer>pstackmax){
|
|
doExecute = false;
|
|
Serial.println("Stackunderflow: PUL");
|
|
}
|
|
}break;
|
|
case 59:{ //JSR
|
|
uint16_t data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
pram[pstackpointer--] = (pframepointer)&0xff;
|
|
pram[pstackpointer--] = (pframepointer>>8)&0xff;
|
|
pram[pstackpointer--] = (pcounter)&0xff;
|
|
pram[pstackpointer--] = (pcounter>>8)&0xff;
|
|
pframepointer = pstackpointer;
|
|
if(pram_length-pstackpointer>pstackmax){
|
|
doExecute = false;
|
|
Serial.println("Stackoverflow: JSR");
|
|
}
|
|
pcounter = data;
|
|
}break;
|
|
case 60:{ //JSA
|
|
uint16_t data = AReg;
|
|
pram[pstackpointer--] = (pframepointer)&0xff;
|
|
pram[pstackpointer--] = (pframepointer>>8)&0xff;
|
|
pram[pstackpointer--] = (pcounter)&0xff;
|
|
pram[pstackpointer--] = (pcounter>>8)&0xff;
|
|
pframepointer = pstackpointer;
|
|
if(pram_length-pstackpointer>pstackmax){
|
|
doExecute = false;
|
|
Serial.println("Stackoverflow: JSA");
|
|
}
|
|
pcounter = data;
|
|
}break;
|
|
case 61:{ //RSR
|
|
pcounter = 0;
|
|
pstackpointer = pframepointer;
|
|
pcounter = pram[++pstackpointer]<<8;
|
|
pcounter += pram[++pstackpointer];
|
|
pframepointer = pram[++pstackpointer]<<8;
|
|
pframepointer += pram[++pstackpointer];
|
|
if(pram_length-pstackpointer>pstackmax){
|
|
doExecute = false;
|
|
Serial.println("Stackunderflow: RSR");
|
|
}
|
|
}break;
|
|
|
|
|
|
case 252:{ //IN
|
|
|
|
}break;
|
|
case 253:{ //OUT
|
|
byte data = prog[pcounter++];
|
|
if(data==1) Serial.println(* (int *) &AReg);
|
|
if(data==0) Serial.println(AReg);
|
|
if(data==2) Serial.println(* (float *) &AReg);
|
|
}break;
|
|
case 254:{ //SHS Set Heap Size
|
|
unsigned short data = prog[pcounter++]<<8;
|
|
data += prog[pcounter++];
|
|
pstackmax = pram_length-data;
|
|
if(pram_length-pstackpointer>pstackmax){
|
|
doExecute = false;
|
|
Serial.println("Stackoverflow: SHS");
|
|
}
|
|
}break;
|
|
case 255:{ //HLT bzw. exit
|
|
pcounter = 0;
|
|
doExecute = false;
|
|
}break;
|
|
}
|
|
}
|