Skip to content

Instantly share code, notes, and snippets.

@ipcjk
Created April 16, 2016 13:02
Show Gist options
  • Save ipcjk/19d444e3acce0a6cd9c21b36ca11e93f to your computer and use it in GitHub Desktop.
Save ipcjk/19d444e3acce0a6cd9c21b36ca11e93f to your computer and use it in GitHub Desktop.
Old basic accumulator from 2007
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
VCPU, Basic ALU
Jörg Kost
[email protected]
*/
#define MEMSIZE 65535
#define CODESIZE 100
/*
0 STORE n n <- (A) Accum. speichern auf Adresse
1 LOAD n A <- (n) Accum. laden mit Wert von Adresse
2 JMPZ n (A)=0 : PC <- n Sprung nach n, falls (A) = 0
3 ADD n A <- (A) + (n) Addiere Wert von n zu A
4 SUB n A <- (A) - (n) Subtrahiere Wert von n von A
5 OR n A <- (A) (n) Bitweise A mit Wert von n verknüpfung
6 AND n A <- (A) (n) Bitweise A mit Wert von n verknüpfung
7 XOR n A <- (A) XOR (n) Bitweise A mit Wert von n verknüpfung
8 NOT A <- NOT(A) A Bitweise A invertieren
9 INC A <- (A) + 1 A inkrementieren
10 DEC A <- (A) - 1 A decrementieren
11 ZERO A <- 0 A mit 0 laden
*/
#define LOAD 1
#define ADD 3
#define STORE 0
#define JMPZ 2
#define SUB 4
#define OR 5
#define AND 6
#define XOR 7
#define INV 8
#define INC 9
#define DEC 10
#define ZERO 11
#define JMP 71
#define JMPNZ 72
#define OUT 70
#define PRESTORE 98
#define HALT 99
int code[CODESIZE][3] = {
/*
{ZERO},
{PRESTORE, 0, 6},
{PRESTORE, 1, 5},
{LOAD, 0},
{OR, 1},
{STORE, 2},
{LOAD, 2},
{OUT},
{INV},
{OUT},
{HALT},
*/
};
int data[MEMSIZE];
struct cpu
{
int eip;
unsigned int A;
} myCpu;
void DATA_reset(int data_ptr[])
{
int i = 0;
for( i = 0; i < MEMSIZE; i++)
{
data_ptr[i] = 0;
}
}
void SYSTEM_RESET(struct cpu *cpu_ptr)
{
cpu_ptr->A = 0x00;
cpu_ptr->eip = 0;
DATA_reset(data);
}
void CPU_RUN(struct cpu *cpu_ptr, int data_ptr[])
{
int OPCODE;
int MEM1;
int MEM2;
while(cpu_ptr->eip < CODESIZE)
{
//printf("EIP: %d OPCODE: %d MEM1 %d MEM2 %d\n", cpu_ptr->eip, code[cpu_ptr->eip][0], code[cpu_ptr->eip][1], code[cpu_ptr->eip][2]);
OPCODE = code[cpu_ptr->eip][0];
MEM1 = code[cpu_ptr->eip][1];
MEM2 = code[cpu_ptr->eip][2];
if(OPCODE == HALT)
{
printf("System halted\n");
break;
}
else if(OPCODE == LOAD)
{
cpu_ptr->A = data_ptr[MEM1];
cpu_ptr->eip++;
}
else if(OPCODE == INC)
{
cpu_ptr->A++;
cpu_ptr->eip++;
}
else if(OPCODE == DEC)
{
cpu_ptr->A--;
cpu_ptr->eip++;
}
else if(OPCODE == STORE)
{
data_ptr[MEM1] = cpu_ptr->A;
cpu_ptr->eip++;
}
else if(OPCODE == OUT)
{
printf("%d\n", cpu_ptr->A);
cpu_ptr->eip++;
}
else if(OPCODE == ZERO)
{
cpu_ptr->A = 0x00;
cpu_ptr->eip++;
}
else if(OPCODE == JMP)
{
cpu_ptr->eip = MEM1;
}
else if(OPCODE == JMPZ)
{
if(cpu_ptr->A == 0x00)
{
cpu_ptr->eip = MEM1;
}
else
{
cpu_ptr->eip++;
}
}
else if(OPCODE == JMPNZ)
{
if(cpu_ptr->A != 0x00)
{
cpu_ptr->eip = MEM1;
}
else
{
cpu_ptr->eip++;
}
}
else if(OPCODE == ADD)
{
cpu_ptr->A += data_ptr[MEM1];
cpu_ptr->eip++;
}
else if(OPCODE == SUB)
{
cpu_ptr->A = cpu_ptr->A - data_ptr[MEM1];
cpu_ptr->eip++;
}
else if(OPCODE == AND)
{
cpu_ptr->A &= data_ptr[MEM1];
cpu_ptr->eip++;
}
else if(OPCODE == OR)
{
cpu_ptr->A |= data_ptr[MEM1];
cpu_ptr->eip++;
}
else if(OPCODE == XOR)
{
cpu_ptr->A ^= data_ptr[MEM1];
cpu_ptr->eip++;
}
else if(OPCODE == INV)
{
cpu_ptr->A =~cpu_ptr->A;
cpu_ptr->eip++;
}
else if(OPCODE == PRESTORE)
{
data_ptr[MEM1] = MEM2;
cpu_ptr->eip++;
}
else
{
printf("ILLEGAL INSTRUCTION\n");
printf("SYSTEM halted\n");
break;
}
}
}
void code_read()
{
int codeline = 0;
char buffer[512];
char *token;
int tokencount = 0;
char **tokenarray = (char **)malloc(sizeof(char) * 3);
while (fgets (buffer, 512, stdin) != NULL) {
tokencount = 0;
tokenarray[0] = NULL;
tokenarray[1] = NULL;
tokenarray[2] = NULL;
buffer [strlen (buffer) - 1] = '\0';
if(strlen(buffer) == 0)
{
continue;
}
token = strtok(buffer, " ");
while(token != NULL)
{
tokenarray[tokencount++] = token;
token = strtok(NULL, ",");
}
if(tokenarray[0] != NULL)
{
if(strncmp(tokenarray[0], "LOAD", 4) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = LOAD;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "OUT", 3) == 0)
{
code[codeline][0] = OUT;
codeline++;
}
else if(strncmp(tokenarray[0], "STORE", 5) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = STORE;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "JMPZ", 4) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = JMPZ;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "SUB", 3) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = SUB;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "OR", 2) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = OR;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "ADD", 3) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = ADD;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "AND", 3) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = AND;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "XOR", 3) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = XOR;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "INV", 3) == 0)
{
code[codeline][0] = INV;
codeline++;
}
else if(strncmp(tokenarray[0], "INC", 3) == 0)
{
code[codeline][0] = INC;
codeline++;
}
else if(strncmp(tokenarray[0], "DEC", 3) == 0)
{
code[codeline][0] = DEC;
codeline++;
}
else if(strncmp(tokenarray[0], "ZERO", 4) == 0)
{
code[codeline][0] = ZERO;
codeline++;
}
else if(strncmp(tokenarray[0], "JMP", 3) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = JMP;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "JMPNZ", 5) == 0 && tokenarray[1] != NULL)
{
code[codeline][0] = JMPNZ;
code[codeline][1] = atoi(tokenarray[1]);
codeline++;
}
else if(strncmp(tokenarray[0], "PRESTORE", 8) == 0 && tokenarray[1] != NULL && tokenarray[2] != NULL)
{
code[codeline][0] = PRESTORE;
code[codeline][1] = atoi(tokenarray[1]);
code[codeline][2] = atoi(tokenarray[2]);
codeline++;
}
else if(strncmp(tokenarray[0], "HALT", 4) == 0)
{
code[codeline][0] = HALT;
codeline++;
}
}
//free(tokenarray);
}
}
int main (int argc, const char * argv[]) {
code_read();
SYSTEM_RESET(&myCpu);
CPU_RUN(&myCpu, data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment