Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define LIMIT 1000
- #define READ 10
- #define SAVE 11
- #define LOAD 20
- #define STORE 21
- #define ADD 30
- #define SUBTRACT 31
- #define DIVIDE 32
- #define MULTIPLY 33
- #define BRANCH 40
- #define BRANCHNEG 41
- #define BRANCHZERO 42
- #define HALT 43
- #define VALID 0
- //errors
- #define DIVIDEZERO 1
- #define INVALID 2
- #define INVALIDWORD 3
- #define INVALIDOPCODE 4
- #define ILLEGALINPUT 5
- #define OVERFLOW 6
- #define PRGTOOBIG 7
- #define UNDERFLOW 8
- int init(int *memory, int *accumulator, int *instructionCounter,
- int *instructionRegister, int *validInstructions, int *opCode,
- int *operand);
- int loadInstruction(int *memory, int *memPointer, int *maxMemory, int *machineOn);
- int executeInstruction(int *instructionRegister, int *memory, int *opCode,
- int *operand, int *instructionCounter,
- int *accumulator, int *validInstructions,
- int *maxMemory, int *machineOn);
- int memDump(int *memory, int *maxMemory);
- int main(void){
- int *memory = (int*) malloc(1000 * (sizeof(int)));
- int x, accumulator, instructionCounter, instructionRegister;
- int opCode, operand, validInstructions;
- int memPointer, machineOn, maxMemory = LIMIT;
- printf("\tSimpletron Virtual Machine\n");
- init(memory, &accumulator, &instructionCounter, &instructionRegister,
- &validInstructions, &opCode, &operand);
- x = loadInstruction(memory, &memPointer, &maxMemory, &machineOn);
- if(x == PRGTOOBIG){
- printf("\n.::ABEND: prg load: prg too big::.\n\n");
- printf("\tRegisters\nAccumulator: %d\nInstruction Counter: %d\n"
- "Instruction Register: %d\nOperation Code: %d\n"
- "Operand: %d\nValid Instruction: %d\n\n", accumulator, instructionCounter,
- instructionRegister, opCode, operand, validInstructions);
- }
- else if(x == INVALIDWORD){
- printf("\n.::ABEND: prg load: invalid word::.\n\n");
- printf("\tRegisters\nAccumulator: %d\nInstruction Counter: %d\n"
- "Instruction Register: %d\nOperation Code: %d\n"
- "Operand: %d\nValid Instruction: %d\n\n", accumulator, instructionCounter,
- instructionRegister, opCode, operand, validInstructions);
- }
- else{
- executeInstruction(&instructionRegister, &memory, &opCode, &operand,
- &instructionCounter, &accumulator, &validInstructions,
- &maxMemory, &machineOn);
- }
- // only continue if machineOn = 1;
- return 0;
- }
- int init(int *memory, int *accumulator, int *instructionCounter, int *instructionRegister,
- int *validInstructions, int *opCode,
- int *operand){
- int i;
- for(i = 0; i < LIMIT; i++){
- memory[i] = 50505;
- }
- *accumulator = 0, *instructionCounter = 0, *instructionRegister = 0;
- *opCode = 0, *operand = 0, *validInstructions = 0;
- return VALID;
- }
- int loadInstruction(int *memory, int *memPointer, int *maxMemory, int *machineOn){
- int word;
- int i = 0;
- while(fscanf(stdin, "%d", &word) != EOF){
- *maxMemory = *memPointer;
- if(word == -999999){
- *machineOn = 1;
- return VALID;
- }
- else if(word < -99999 || word > 99999){
- return INVALIDWORD;
- }
- if(i >= LIMIT){
- return PRGTOOBIG;
- }
- memory[i] = word;
- i++;
- *memPointer += 1;
- }
- }
- int executeInstruction(int *instructionRegister, int *memory, int *opCode, int *operand,
- int *instructionCounter, int *accumulator, int *validInstructions,
- int *maxMemory, int *machineOn){
- int i;
- int a;
- int done = 0;
- while (done != 1)
- {
- /* instruction fetch */
- if(0 <= *instructionCounter && *instructionCounter <= 999){
- instructionRegister = memory[*instructionCounter];
- *opCode = *instructionRegister / 1000;
- *operand = *instructionRegister % 1000;
- }
- else if(*instructionCounter < 0 || *instructionCounter > 999){
- printf(".::ABEND: Addressability error::.\n");
- done = 1;
- }
- //instruction execute
- switch(*opCode){
- case READ:
- fscanf(stdin, "%d", &a);
- if(a < -99999 || a > 99999){
- return ILLEGALINPUT;
- }
- mem[*operand] = a;
- if(mem[*operand] < 0)
- else
- break;
- case SAVE:
- break;
- case LOAD:
- break;
- case STORE:
- break;
- case ADD:
- break;
- case SUBTRACT:
- break;
- case MULTIPLY:
- break;
- case DIVIDE:
- break;
- case BRANCH:
- break;
- case BRANCHNEG:
- break;
- case BRANCHZERO:
- break;
- case HALT:
- done = 1;
- break;
- default:
- return INVALIDOPCODE;
- break;
- }
- if(done != 1){
- instructionCounter++;
- }
- /*if (operationCode is not branching AND done != 1)
- instructionCounter++;
- } end while */
- }
- }
- //"C:\Users\Omid\clanp\assign1\All\03_NORMAL_ADDITION.txt"
- int memDump(int *memory, int *maxMemory){
- }
Add Comment
Please, Sign In to add comment