#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Go to the source code of this file.
Macros | |
#define | MAX_PROG_LEN 99 |
#define | MAX_LINE_LEN 80 |
#define | MAX_OPCODE 10 |
#define | MAX_REGISTER 4 |
#define | OPCODE_LENGTH 3 |
#define | ARG_LENGTH 4 |
#define | OPCODE 0 |
#define | ARG1 1 |
#define | ARG2 2 |
Functions | |
int | opcodeNOP (char *opcode, char *arg1, char *arg2) |
int | opcodeSET (char *opcode, char *arg1, char *arg2) |
int | opcodeAND (char *opcode, char *arg1, char *arg2) |
int | opcodeOR (char *opcode, char *arg1, char *arg2) |
int | opcodeADD (char *opcode, char *arg1, char *arg2) |
int | opcodeSUB (char *opcode, char *arg1, char *arg2) |
int | opcodeSHL (char *opcode, char *arg1, char *arg2) |
int | opcodeSHR (char *opcode, char *arg1, char *arg2) |
int | opcodeJMP (char *opcode, char *arg1, char *arg2) |
int | opcodePRT (char *opcode, char *arg1, char *arg2) |
int | getArg2 (char *arg2) |
bool | isValid (char *opcode, char *arg1, char *arg2) |
int | execInstruction (char *instruction) |
int | execProgram () |
int | loadProgram () |
This file is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Header file for emulator.c
.
int execInstruction | ( | char * | instruction | ) |
Executes an instruction.
Tests the validity of an instruction (and whether or not it's a comment), and if the line passes, calls the appropriate function.
instruction | the instruction to test |
int execProgram | ( | ) |
Executes an the program.
Runs execInstruction
on each line of the program in turn.
int getArg2 | ( | char * | arg2 | ) |
A function to return the value in the second argument of an instruction.
Returns either the value in the indicated register or, as the instruction is parsed as a string, the integer value given. mystrncmp()
is used over mystrcmp()
because there tends to be dodgy C string magic tacked on to the end of the args.
arg2 | the value to parse |
bool isValid | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
Tests if an instruction is valid.
Tests that the opcode is valid and that the registers are also.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument (not applicable for PRT ) |
true
if the string is valid, false
if it is not int loadProgram | ( | ) |
Loads a program.
Loads the program file and reads each line into an array.
int opcodeADD | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the ADD
opcode
Performs addition on the values in arg1
and arg2
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeAND | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the AND
opcode
Performs a bitwise AND on the values in arg1
and arg2
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeJMP | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the JMP
opcode
If REGX
contains 0, jumps to the line indicated by arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument (not applicable for JMP ) |
int opcodeNOP | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the NOP
opcode
Increments the instruction pointer INSP
.
opcode | the opcode |
arg1 | the first argument (not applicable for NOP ) |
arg2 | the second argument (not applicable for NOP ) |
int opcodeOR | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the OR
opcode
Performs a bitwise OR on the values in arg1
and arg2
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodePRT | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the PRT
opcode
Prints the value in the register indicated in arg1
, or the integer value specified.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument (not applicable for PRT ) |
int opcodeSET | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SET
opcode
Gets the value of either the register or integer set in arg2
and places it in the register indicated by arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeSHL | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SHL
opcode
Performs a bitwise shift left on the value in arg1
, storing the result in arg1
. The number of shifts is specified in arg2
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeSHR | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SHR
opcode
Performs a bitwise shift right on the value in arg1
, storing the result in arg1
. The number of shifts is specified in arg2
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |
int opcodeSUB | ( | char * | opcode, |
char * | arg1, | ||
char * | arg2 | ||
) |
A function to handle the SUB
opcode
Subtracts the value in arg2
from the value in arg1
, storing the result in arg1
.
opcode | the opcode |
arg1 | the first argument |
arg2 | the second argument |