Skip to content

Commit

Permalink
add branch ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Psmths committed Nov 20, 2021
1 parent 7587ed7 commit 6051740
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 17 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ clean:
@echo "Cleaning project"
rm -rf $(BIN_DIR)

examples: helloworld adder xor_cipher
examples: helloworld adder xor_cipher branch_test fault_test

helloworld:
@echo "Building example program 1 - helloworld.asm"
Expand All @@ -22,11 +22,22 @@ adder:
$(CC) $(wildcard $(EXAMPLES_DIR)/02*/*.cpp) $(wildcard $(SOURCE_DIR)/*.cpp) -o $(BIN_DIR)/02_adder $(CFLAGS)

xor_cipher:
@echo "Building example program 2 - my_adder.asm"
@echo "Building example program 3 - xor_cipher.asm"
$(MD) $(BIN_DIR)
$(CC) $(wildcard $(EXAMPLES_DIR)/03*/*.cpp) $(wildcard $(SOURCE_DIR)/*.cpp) -o $(BIN_DIR)/03_xor_cipher $(CFLAGS)

branch_test:
@echo "Building example program 4 - branch_test.asm"
$(MD) $(BIN_DIR)
$(CC) $(wildcard $(EXAMPLES_DIR)/04*/*.cpp) $(wildcard $(SOURCE_DIR)/*.cpp) -o $(BIN_DIR)/04_branch_test $(CFLAGS)

fault_test:
@echo "Building example program 5 - fault_test.asm"
$(MD) $(BIN_DIR)
$(CC) $(wildcard $(EXAMPLES_DIR)/05*/*.cpp) $(wildcard $(SOURCE_DIR)/*.cpp) -o $(BIN_DIR)/05_fault_test $(CFLAGS)

test:
./$(BIN_DIR)/01_helloworld
./$(BIN_DIR)/02_adder
./$(BIN_DIR)/03_xor_cipher
./$(BIN_DIR)/05_fault_test
6 changes: 5 additions & 1 deletion examples/01_hello_world/hello_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ int main() {

my_cpu->load_rom(rom, sizeof(rom));
unsigned char* result = my_cpu->run();
std::cout << result << std::endl;
if (result) {
std::cout << result << std::endl;
} else {
return 1;
}
return 0;
}
4 changes: 3 additions & 1 deletion examples/02_my_adder/my_adder.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
my_adder.cpp - Example program that passes two 16-bit unsigned integers
to the RISCAL stack, adds them, and returns them to the stack.
outp | addr | data
0:0 | 0 | 00 90 00 01 ; POP_WORD R1
4:0 | 4 | 00 90 00 02 ; POP_WORD R2
Expand Down Expand Up @@ -47,6 +47,8 @@ int main() {
uint32_t result_cast;
memcpy(&result_cast, result, sizeof(x));
std::cout << std::dec << result_cast << std::endl;
} else {
return 1;
}

return 0;
Expand Down
7 changes: 4 additions & 3 deletions examples/03_xor_cipher/xor_cipher.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
xor_cipher.cpp - Example program that passes arbitrary-length char array
to RISCAL and encrypts it with a XOR cipher. Returns raw ciphertext.
outp | addr | data
0:0 | 0 | 10 03 08 00 ; MOVE_LOWER R3, 2048
4:0 | 4 | ; fetch_stack:
Expand Down Expand Up @@ -43,7 +43,6 @@ int main() {

RISCAL_CPU *my_cpu = new RISCAL_CPU();

/* Adding program */
unsigned char rom[] = {
/* 0x00 */ 0x10, 0x03, 0x08, 0x00, 0x00, 0xb0, 0x00, 0x02, 0x0e, 0x00, 0x00, 0x23, 0x00, 0x50, 0x00, 0x03,
/* 0x10 */ 0x02, 0x00, 0x00, 0x20, 0x70, 0x00, 0x00, 0x04, 0x10, 0x03, 0x08, 0x00, 0x10, 0x04, 0x00, 0x10,
Expand All @@ -54,7 +53,7 @@ int main() {
/* 0x60 */ 0x59, 0x51, 0xb7, 0x57, 0x5c, 0xa1, 0xf5, 0x47, 0xdf, 0xfe, 0x61, 0x3a, 0x34, 0xe1, 0x35, 0x95
};

/* Copy integers to the stack */
/* Copy chars to the stack */
unsigned char my_stack[] = {0x00, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6f,
/* 0x10 */ 0x66, 0x20, 0x61, 0x20, 0x73, 0x69, 0x6e, 0x67, 0x6c, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x58,
/* 0x20 */ 0x4f, 0x52, 0x20, 0x63, 0x69, 0x70, 0x68, 0x65, 0x72};
Expand All @@ -71,6 +70,8 @@ int main() {
/* Print result if not FAULT status */
if (result) {
std::cout << std::hex << result << std::endl;
} else {
return 1;
}

return 0;
Expand Down
32 changes: 32 additions & 0 deletions examples/04_branch_test/branch_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
main:
MOVE_LOWER R15, test_fail ; Pointer to fail
MOVE_LOWER R14, test2 ; Pointer to test2

test1:
MOVE_LOWER R1, 25
MOVE_LOWER R2, 100

COMPARE R1, R2
JUMP_E test_fail ; Test jump if equal imm
JUMP_E R15 ; Test jump if equal r
JUMP R14 ; Test jump r
FAULT

test2:
COMPARE R1, R2
JUMP_GT test_fail ; Test jump if greater imm
JUMP_GT R15 ; Test jump if greater r
JUMP_LT test3
FAULT

test3:
COMPARE R2, R1
JUMP_LT test_fail ; Test jump if less imm
JUMP_LT R15 ; Test jump if less r
JUMP_GT test_succeed ; All checks passed

test_fail:
HALT

test_succeed:
HALT
67 changes: 67 additions & 0 deletions examples/04_branch_test/branch_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
branch_test.cpp - Tests all branch instructions
main:
MOVE_LOWER R15, test_fail ; Pointer to fail
MOVE_LOWER R14, test2 ; Pointer to test2
test1:
MOVE_LOWER R1, 25
MOVE_LOWER R2, 100
COMPARE R1, R2
JUMP_E test_fail ; Test jump if equal imm
JUMP_E R15 ; Test jump if equal r
JUMP R14 ; Test jump r
FAULT
test2:
COMPARE R1, R2
JUMP_GT test_fail ; Test jump if greater imm
JUMP_GT R15 ; Test jump if greater r
JUMP test3 ; Test jump imm
FAULT
test3:
COMPARE R2, R1
JUMP_LT test_fail ; Test jump if less imm
JUMP_LT R15 ; Test jump if less r
JUMP test_succeed ; All checks passed
test_fail:
HALT
test_succeed:
HALT
*/

#include "../../include/cpu.hpp"
#include <iostream>
#include <cstring>

int main() {

RISCAL_CPU *my_cpu = new RISCAL_CPU();

/* Adding program */
unsigned char rom[] = {
/* 0x00 */ 0x10, 0x0f, 0x00, 0x48, 0x10, 0x0e, 0x00, 0x24, 0x10, 0x01, 0x00, 0x19, 0x10, 0x02, 0x00, 0x64,
/* 0x10 */ 0x02, 0x00, 0x00, 0x12, 0x80, 0x00, 0x00, 0x48, 0x00, 0xd0, 0x00, 0x0f, 0x01, 0xf0, 0x00, 0x0e,
/* 0x20 */ 0x00, 0x30, 0x00, 0x00, 0x02, 0x00, 0x00, 0x12, 0x90, 0x00, 0x00, 0x48, 0x00, 0xe0, 0x00, 0x0f,
/* 0x30 */ 0xa0, 0x00, 0x00, 0x38, 0x00, 0x30, 0x00, 0x00, 0x02, 0x00, 0x00, 0x21, 0xa0, 0x00, 0x00, 0x48,
/* 0x40 */ 0x00, 0xf0, 0x00, 0x0f, 0x90, 0x00, 0x00, 0x4c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00
};

/* Load the PROM to RISCAL */
my_cpu->load_rom(rom, sizeof(rom));

/* Run RISCAL */
unsigned char* result = my_cpu->run();

if (!result) {
return 1;
}

return 0;
}
2 changes: 2 additions & 0 deletions examples/05_test_fault/fault.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main:
FAULT
32 changes: 32 additions & 0 deletions examples/05_test_fault/test_fault.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
test_fault.cpp - test RISCAL fault state
main:
FAULT
*/

#include "../../include/cpu.hpp"
#include <iostream>
#include <cstring>

int main() {

RISCAL_CPU *my_cpu = new RISCAL_CPU();

unsigned char rom[] = {
/* 0x0 */ 0x00, 0x30, 0x00, 0x00
};

/* Load the PROM to RISCAL */
my_cpu->load_rom(rom, sizeof(rom));

/* Run RISCAL */
unsigned char* result = my_cpu->run();

if (result) {
return 1;
}

return 0;
}
3 changes: 2 additions & 1 deletion examples/riscal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
POP_BYTE R{reg} => 0x00B0000 @ reg`4
JUMP_NE R{reg} => 0x00C0000 @ reg`4
JUMP_E R{reg} => 0x00D0000 @ reg`4
JUMPT_GT R{reg} => 0x00E0000 @ reg`4
JUMP_GT R{reg} => 0x00E0000 @ reg`4
JUMP_LT R{reg} => 0x00F0000 @ reg`4
JUMP R{reg} => 0x01F0000 @ reg`4
CALL R{reg} => 0x0100000 @ reg`4
COMPARE R{reg1}, R{reg2} => 0x020000 @ reg1`4 @ reg2`4
ADD R{reg1}, R{reg2} => 0x030000 @ reg1`4 @ reg2`4
Expand Down
18 changes: 14 additions & 4 deletions include/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ class RISCAL_CPU {
void op_pop_byte(cpu_word data);
void op_jump_ne(cpu_word data);
void op_jump_e(cpu_word data);
//void op_jump_gt(cpu_word data);
//void op_jump_lt(cpu_word data);
void op_jump_gt(cpu_word data);
void op_jump_lt(cpu_word data);
void op_jump(cpu_word data);
void op_call(cpu_word data);

/* Double-register opcodes */
Expand All @@ -83,11 +84,13 @@ class RISCAL_CPU {
void op_move_upper(cpu_word data);
void op_move_lower(cpu_word data);
void op_load_word_imm(cpu_word data);
//void op_store_word_imm(cpu_word data);
void op_store_word_imm(cpu_word data);
void op_load_byte_imm(cpu_word data);
//void op_store_byte_imm(cpu_word data);
void op_store_byte_imm(cpu_word data);
void op_jump_ne_imm(cpu_word data);
void op_jump_e_imm(cpu_word data);
void op_jump_gt_imm(cpu_word data);
void op_jump_lt_imm(cpu_word data);
void op_jump_imm(cpu_word data);

typedef void (RISCAL_CPU::*op_f)(cpu_word data);
Expand All @@ -105,6 +108,9 @@ class RISCAL_CPU {
{POP_BYTE, &RISCAL_CPU::op_pop_byte},
{JUMP_NE, &RISCAL_CPU::op_jump_ne},
{JUMP_E, &RISCAL_CPU::op_jump_e},
{JUMP_GT, &RISCAL_CPU::op_jump_gt},
{JUMP_LT, &RISCAL_CPU::op_jump_lt},
{JUMP, &RISCAL_CPU::op_jump},
{CALL, &RISCAL_CPU::op_call},
{COMPARE, &RISCAL_CPU::op_compare},
{ADD, &RISCAL_CPU::op_add},
Expand All @@ -122,9 +128,13 @@ class RISCAL_CPU {
{MOVE_LOWER, &RISCAL_CPU::op_move_lower},
{MOVE_UPPER, &RISCAL_CPU::op_move_upper},
{LOAD_WORD_IMM, &RISCAL_CPU::op_load_word_imm},
{STORE_WORD_IMM, &RISCAL_CPU::op_store_word_imm},
{LOAD_BYTE_IMM, &RISCAL_CPU::op_load_byte_imm},
{STORE_BYTE_IMM, &RISCAL_CPU::op_store_byte_imm},
{JUMP_NE_IMM, &RISCAL_CPU::op_jump_ne_imm},
{JUMP_E_IMM, &RISCAL_CPU::op_jump_e_imm},
{JUMP_GT_IMM, &RISCAL_CPU::op_jump_gt_imm},
{JUMP_LT_IMM, &RISCAL_CPU::op_jump_lt_imm},
{JUMP_IMM, &RISCAL_CPU::op_jump_imm}
};

Expand Down
2 changes: 1 addition & 1 deletion include/debugger.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef DBG_HPP
#define DBG_HPP

#define DEBUG
//#define DEBUG

#endif
1 change: 1 addition & 0 deletions include/opcode_lut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define JUMP_E 0x00D
#define JUMP_GT 0x00E
#define JUMP_LT 0x00F
#define JUMP 0x01F
#define CALL 0x010
#define COMPARE 0x020
#define ADD 0x030
Expand Down
Loading

0 comments on commit 6051740

Please sign in to comment.