Commit 543dbe8b authored by sea19's avatar sea19
Browse files

Print function should be printing out correct output

parent bc050003
......@@ -5,6 +5,7 @@
#include "decode.h"
#include "fetch.h"
#include "execute.h"
#include <math.h>
#define NREGS 17
#define STACK_SIZE 16384 //Defined stack as ints which are 4 bytes so (64*1024)/4
......@@ -12,24 +13,33 @@
void bigToLittleEndian(armstate *state) {
int temp;
for (int i = 0; i < NREGS - 2; i++) {
temp = state->regs[i];
state->regs[i] = (temp & 0xff) << 24 | (temp & 0xff00) << 8 | (temp & 0xff0000) >> 8 | (temp & 0xff000000) >> 24;
}
for (int j = 0; j < STACK_SIZE; j++) {
temp = state->memory[j];
state->memory[j] = (temp & 0xff) << 24 | (temp & 0xff00) << 8 | (temp & 0xff0000) >> 8 | (temp & 0xff000000) >> 24;
state->memory[j] = (temp & 0xff) << 24 | (temp & 0xff00) << 8 |
(temp & 0xff0000) >> 8 | (temp & 0xff000000) >> 24;
}
}
void printResult(armstate *state, int numOfInstr) {
bigToLittleEndian(state);
printf("Registers:\n");
for (int i = 0; i < 13; i++) {
printf("$%d : %d (%x)\n", i, state->regs[i], state->regs[i]);
if (i < 10) {
printf("$%d :%11d (0x%08x)\n", i, state->regs[i], state->regs[i]);
} else {
printf("$%d :%11d (0x%08x)\n", i, state->regs[i], state->regs[i]);
}
}
printf("PC : %d (%x)\n", state->regs[PC], state->regs[PC]);
printf("CPSR : %d (%x)\n", state->regs[16], state->regs[16]);
printf("PC :%11d (0x%08x)\n", state->regs[PC], state->regs[PC]);
if (state->regs[16] >= pow(2, 31)) {
printf("CPSR:%12d (0x%08x)\n", state->regs[16], state->regs[16]);
} else {
printf("CPSR:%11d (0x%08x)\n", state->regs[16], state->regs[16]);
}
printf("Non-zero memory:\n");
for (int i = 0; i < ((state->regs[PC] / 4) - 2); i++) {
printf("0x%08x: 0x%08x\n", i * 4, state->memory[i]);
}
}
void startCycle(armstate *state) {
......@@ -49,23 +59,27 @@ void startCycle(armstate *state) {
*pc += 4;
} else {
execute(decodedInstr, state);
if (decodedInstr->type == branch && do_next_instruction(decodedInstr, state)) {
if (decodedInstr->type == branch && do_next_instruction(decodedInstr,
state)) {
fetched = fetch(objectcode, *pc/4);
*pc += 4;
}
decode(fetched, decodedInstr);
fetched = fetch(objectcode, *pc/4);
*pc += 4;
if (decodedInstr->condition == 0 && decodedInstr->bit0to25 == 0 && decodedInstr->type == and){
if (decodedInstr->condition == 0 && decodedInstr->bit0to25 == 0 &&
decodedInstr->type == and){
finished = true;
counter -= 2;
}
}
state->regs[16] = state->n << 31 | state->z << 30 | state->c << 29 | state->v << 28;
state->regs[16] = state->n << 31 | state->z << 30 | state->c << 29 |
state->v << 28;
counter++;
}
printResult(state, counter);
free(decodedInstr);
} //pipeline
......@@ -85,4 +99,3 @@ int main(int argc, char **argv) {
free(state);
return EXIT_SUCCESS;
}
......@@ -130,36 +130,37 @@ void decode_data_processing(decoded *decodedInstr, decoded_dp *decodedDp, armsta
void execute(decoded *decodedInstr, armstate *state){
if(do_next_instruction(decodedInstr, state)){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
switch(decodedInstr -> type){
case and:
and_instr(decodedInstr, state);
and_instr(decodedInstr, state, decodedDp);
break;
case eor:
eor_instr(decodedInstr, state);
eor_instr(decodedInstr, state, decodedDp);
break;
case sub:
sub_instr(decodedInstr, state);
sub_instr(decodedInstr, state, decodedDp);
break;
case rsb:
rsb_instr(decodedInstr, state);
rsb_instr(decodedInstr, state, decodedDp);
break;
case add:
add_instr(decodedInstr, state);
add_instr(decodedInstr, state, decodedDp);
break;
case tst:
tst_instr(decodedInstr, state);
tst_instr(decodedInstr, state, decodedDp);
break;
case teq:
teq_instr(decodedInstr, state);
teq_instr(decodedInstr, state, decodedDp);
break;
case cmp:
cmp_instr(decodedInstr, state);
cmp_instr(decodedInstr, state, decodedDp);
break;
case orr:
orr_instr(decodedInstr, state);
orr_instr(decodedInstr, state, decodedDp);
break;
case mov:
mov_instr(decodedInstr, state);
mov_instr(decodedInstr, state, decodedDp);
break;
case multiply:
multiply_instr(decodedInstr, state);
......@@ -171,6 +172,7 @@ void execute(decoded *decodedInstr, armstate *state){
single_data_transfer_instr(decodedInstr, state);
break;
}
free(decodedDp);
}
//state->regs[PC] += 4;
}
......
#include "execute.h"
#include <stdlib.h>
#include <stdio.h>
void and_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void and_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = (state -> regs[decodedDp -> rn] & decodedDp -> operand2);
......@@ -17,11 +17,9 @@ void and_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void eor_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void eor_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = (state -> regs[decodedDp -> rn] ^ decodedDp -> operand2);
......@@ -36,11 +34,9 @@ void eor_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void sub_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void sub_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = (state -> regs[decodedDp -> rn] - decodedDp -> operand2);
......@@ -59,11 +55,9 @@ void sub_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void rsb_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void rsb_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = decodedDp -> operand2 - state -> regs[decodedDp -> rn];
......@@ -82,11 +76,9 @@ void rsb_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void add_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void add_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = decodedDp -> operand2 + state -> regs[decodedDp -> rn];
......@@ -105,11 +97,9 @@ void add_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void tst_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void tst_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = decodedDp -> operand2 & state -> regs[decodedDp -> rn];
......@@ -127,11 +117,9 @@ void tst_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void teq_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void teq_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned res = (state -> regs[decodedDp -> rn] ^ decodedDp -> operand2);
......@@ -145,11 +133,9 @@ void teq_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void cmp_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void cmp_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned res = (state->regs[decodedDp->rn] - decodedDp->operand2);
......@@ -167,11 +153,9 @@ void cmp_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void orr_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void orr_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
unsigned int res = (state -> regs[decodedDp -> rn] | decodedDp -> operand2);
......@@ -186,11 +170,9 @@ void orr_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
void mov_instr(decoded *decodedInstr, armstate *state){
decoded_dp *decodedDp = calloc(1, sizeof(decoded_dp));
void mov_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp){
decode_data_processing(decodedInstr, decodedDp, state);
state -> regs[decodedDp -> rd] = decodedDp -> operand2;
......@@ -204,7 +186,6 @@ void mov_instr(decoded *decodedInstr, armstate *state){
state -> z = 0;
}
}
free(decodedDp);
}
......@@ -248,12 +229,20 @@ void single_data_transfer_instr(decoded *decodedInstr, armstate *state){
if(load_bit == 1) {
char *byteMem = (char *) state->memory;
int *memAdd = (int *)(byteMem + data);
state->regs[rd] = *memAdd;
if (data <= 65535) {
state->regs[rd] = *memAdd;
} else {
printf("Error: Out of bounds memory access at address 0x%x\n", data);
}
} else {
/*char *byteMem = (char*) state->memory;
char *byteMem = (char*) state->memory;
char *memAdd;
memAdd = byteMem + data;
*memAdd = state->regs[rn];*/
if ((data <= 65535 && immediate == 0) || immediate == 1) {
*memAdd = state->regs[rn];
} else {
printf("Error: Out of bounds memory access at address 0x%x\n", data);
}
}
}
......
#ifndef INSTRTYPE_H
#define INSTRTYPE_H
void and_instr(decoded *decodedInstr, armstate *state); //Executes an and instruction
void and_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes an and instruction
void eor_instr(decoded *decodedInstr, armstate *state); //Executes an eor instruction
void eor_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes an eor instruction
void sub_instr(decoded *decodedInstr, armstate *state); //Executes a sub instruction
void sub_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes a sub instruction
void rsb_instr(decoded *decodedInstr, armstate *state); //Executes a rsb instruction
void rsb_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes a rsb instruction
void add_instr(decoded *decodedInstr, armstate *state); //Executes an add instruction
void add_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes an add instruction
void tst_instr(decoded *decodedInstr, armstate *state); //Executes a tst instruction
void tst_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes a tst instruction
void teq_instr(decoded *decodedInstr, armstate *state); //Executes a teq instruction
void teq_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes a teq instruction
void cmp_instr(decoded *decodedInstr, armstate *state); //Executes a cmp instruction
void cmp_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes a cmp instruction
void orr_instr(decoded *decodedInstr, armstate *state); //Executes an orr instruction
void orr_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes an orr instruction
void mov_instr(decoded *decodedInstr, armstate *state); //Executes a mov instruction
void mov_instr(decoded *decodedInstr, armstate *state, decoded_dp *decodedDp); //Executes a mov instruction
void single_data_transfer_instr(decoded *decodedInstr, armstate *state); //Executes a single datat transfer instruction
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment