Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattixtech committed Aug 1, 2018
1 parent 8b3e227 commit c30693f
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 142 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ project(mbvm C)
set(CMAKE_C_STANDARD 11)

# Create an exec directory to store the build products in
set(EXEC_DIRECTORY ../exec)
set(EXEC_DIRECTORY ${PROJECT_SOURCE_DIR}/exec)
file(MAKE_DIRECTORY ${EXEC_DIRECTORY})
add_custom_target(create-exec-dir ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${EXEC_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXEC_DIRECTORY})

# TODO: Find a better way to organize the sourc files here
set(SOURCES_MAIN
src/instructions/instruction_exec.c
src/instructions/instruction_exec.h
Expand All @@ -34,8 +35,10 @@ set(SOURCES_TEST
tests/system/test_system.h
tests/vm/test_vm.c
tests/vm/test_vm.h
tests/test_registry.c)
set(SOURCES_TEST_REGISTRY)
tests/test_utils.c
tests/test_utils.h)

set(SOURCES_TEST_REGISTRY tests/test_registry.c)

# Main executable
add_executable(mbvm ${SOURCES_MAIN} ${SOURCES_MAIN_MBVM})
Expand Down
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
MBVM
===

About
---
##About
+ See the doc/ directory.

Prerequisites
---
+ CMake
##Building
###Prerequisites
+ CMake >= 3.10
+ `sudo yum install cmake3` or `sudo apt install cmake` (also available via cygwin)
+ CUnit (if testing) (http://cunit.sourceforge.net/)
+ `sudo yum install CUnit CUnit-devel` or `sudo apt install libcunit1 libcunit1-dev`

Building
---
###Building with CMake
+ Run CMake in the project's root directory
+ `cmake .`
+ Run the appropriate make target depending on if you want to test or not
+ `make mbvm`
+ `make mbvm_test`

Running
---
##Running
+ TODO

Testing
---
##Testing
+ Make sure the test executable has been built (see above)
+ Run the test make target
+ `make test`
Expand Down
15 changes: 5 additions & 10 deletions doc/vm.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
VM Details
===
#VM Details

Machine Layout
---
##Machine Layout
+ Program Counter: PC - DWORD
+ Stack Pointer: SP - DWORD
+ Registers: r0-r7 - DWORD
+ Data Register: dr - DWORD
+ Print Register: pr - DWORD
+ Status Register: sr - BYTE

Machine Instructions
---
##Machine Instructions
+ EXIT
+ NOP
+ PUSH
Expand All @@ -25,12 +22,10 @@ Machine Instructions
+ FCAL
+ FRET

Advanced Instructions
---
##Advanced Instructions
+ PRINT
+ SCAN

Machine Features
--
##Virtual Machine Features
+ Print-to-screen
+ Accessed by storing a character or string address in the print register and then issuing the PRINT instruction
106 changes: 54 additions & 52 deletions src/instructions/instruction_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
struct instruction dec_instr(uint32_t instr)
{
struct instruction ic =
{
// op is the most significant byte of the instruction
.op = (char)((instr & 0xFF000000) >> 24),
// mode is the second most significant byte of the instruction
.mode = (char)((instr & 0x00FF0000) >> 16),
// d1 is the second least significant byte of the instruction
.d1 = (char)((instr & 0x0000FF00) >> 8),
// d2 is the least significant byte of the instruction
.d2 = (char)(instr & 0x000000FF),
// data is the least significant 2 bytes of the instruction
.data = (instr & 0x0000FFFF),
};
{
// op is the most significant byte of the instruction
.op = (uint8_t) ((instr & 0xFF000000) >> 24),
// mode is the second most significant byte of the
// instruction
.mode = (uint8_t) ((instr & 0x00FF0000) >> 16),
// d1 is the second least significant byte of the
// instruction
.d1 = (uint8_t) ((instr & 0x0000FF00) >> 8),
// d2 is the least significant byte of the instruction
.d2 = (uint8_t) (instr & 0x000000FF),
// data is the least significant 2 bytes of the instruction
.data = (instr & 0x0000FFFF),
};

return ic;
}
Expand All @@ -48,47 +50,47 @@ int exec_instr(struct instruction ic)
switch (ic.op)
{
// Regular Instructions...
case INSTR_EXIT:
instr_exit();
case INSTR_EXIT:
instr_exit();

return 1;
case INSTR_LOAD:
instr_load(ic);
break;
case INSTR_STORE:
instr_store(ic);
break;
case INSTR_ADD:
instr_add(ic);
break;
case INSTR_SUB:
instr_sub(ic);
break;
case INSTR_PUSH:
instr_push(ic);
break;
case INSTR_POP:
instr_pop(ic);
break;
case INSTR_PEEK:
instr_peek(ic);
break;
case INSTR_JMP:
instr_jmp(ic);
break;
case INSTR_FCAL:
instr_function_call();
break;
case INSTR_FRET:
instr_function_return();
break;
// Advanced Instructions...
case ADV_INSTR_PRINT:
instr_adv_print(ic);
break;
case ADV_INSTR_SCAN:
instr_adv_scan(ic);
break;
return 1;
case INSTR_LOAD:
instr_load(ic);
break;
case INSTR_STORE:
instr_store(ic);
break;
case INSTR_ADD:
instr_add(ic);
break;
case INSTR_SUB:
instr_sub(ic);
break;
case INSTR_PUSH:
instr_push(ic);
break;
case INSTR_POP:
instr_pop(ic);
break;
case INSTR_PEEK:
instr_peek(ic);
break;
case INSTR_JMP:
instr_jmp(ic);
break;
case INSTR_FCAL:
instr_function_call();
break;
case INSTR_FRET:
instr_function_return();
break;
// Advanced Instructions...
case ADV_INSTR_PRINT:
instr_adv_print(ic);
break;
case ADV_INSTR_SCAN:
instr_adv_scan(ic);
break;
}

return 0;
Expand Down
18 changes: 12 additions & 6 deletions src/system/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

/**
* Executes an instruction.
*
* @param programCode TODO
* @return TODO
*/
int exec(uint32_t programCode)
{
Expand All @@ -26,7 +29,10 @@ int exec(uint32_t programCode)
}

/**
* Returns the 4byte block found in loc @ image_addr.
* Returns the 4 byte block found in loc @ image_addr.
*
* @param loc TODO
* @param image_addr TODO
*/
uint32_t get_dword(uint8_t *loc, uint32_t image_addr)
{
Expand All @@ -39,7 +45,7 @@ uint32_t get_dword(uint8_t *loc, uint32_t image_addr)
}

/**
* Stores a 4byte value into a memory location.
* Stores a 4 byte value into a memory location.
*/
void store_dword(uint8_t *loc, uint32_t index, uint32_t block)
{
Expand All @@ -50,7 +56,7 @@ void store_dword(uint8_t *loc, uint32_t index, uint32_t block)
}

/**
* Stores a 2byte value into a memory location.
* Stores a 2 byte value into a memory location.
*/
void store_word(uint8_t *loc, uint32_t index, uint16_t word)
{
Expand All @@ -67,7 +73,7 @@ void store_byte(uint8_t *loc, uint32_t index, uint8_t byte)
}

/**
* Returns the 2byte word found in loc @ image_addr.
* Returns the 2 byte word found in loc @ image_addr.
*/
uint16_t get_word(uint8_t *loc, uint32_t image_addr)
{
Expand Down Expand Up @@ -118,7 +124,7 @@ void disp_image(uint32_t *image, int blocks)
for (int i = 0; i < blocks; i++)
{
// Take the 4 bytes in image[i] and get a char pointer to them
unsigned char *chars = (unsigned char *)&image[i];
unsigned char *chars = (unsigned char *) &image[i];
printf("0x%X \t" FORMAT_HEX_4_BYTE "\t%c%c%c%c\n",
i * INSTRUCTION_SIZE, image[i], chars[3], chars[2], chars[1],
chars[0]);
Expand Down Expand Up @@ -154,7 +160,7 @@ void dump_state()
int printed_contents = 0;

for (int i = flash_allocated * INSTRUCTION_SIZE;
i < ((int)configured_ram_size); i++)
i < ((int) configured_ram_size); i++)
{
char ram_contents = *(ram + i);

Expand Down
4 changes: 2 additions & 2 deletions src/system/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
*/

#include <stdint.h>

#ifndef SYSTEM_H
#define SYSTEM_H

#define EMPTY_BYTE 0x00
#define FORMAT_HEX_4_BYTE "0x%08X"

uint8_t debugging;
int exec(uint32_t);
void disp_image(uint32_t *, int);
Expand All @@ -22,6 +21,7 @@ void store_byte(uint8_t *, uint32_t, uint8_t);
uint32_t get_dword(uint8_t *, uint32_t);
uint16_t get_word(uint8_t *, uint32_t);
uint8_t get_byte(uint8_t *, uint32_t);
// TODO: Unused
uint32_t offset_ram_for_flash(uint32_t);
uint32_t incr_pc();
#endif
21 changes: 12 additions & 9 deletions src/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ void load_test_app()
{
// Hello world...
uint32_t program[] =
{
create_instruction(INSTR_LOAD, MODE_IMMEDIATE_B, EMPTY_BYTE, 0x10),
create_instruction(INSTR_STORE, MODE_REGISTER, EMPTY_BYTE, 0x09),
create_instruction(ADV_INSTR_PRINT, MODE_EXTRA, EMPTY_BYTE,
EMPTY_BYTE),
create_instruction(INSTR_EXIT, EMPTY_BYTE, EMPTY_BYTE, EMPTY_BYTE),
0x68656C6C, // The string 'hello world\n'
0x6F20776F,
0x726C640A};
{
create_instruction(INSTR_LOAD, MODE_IMMEDIATE_B, EMPTY_BYTE,
0x10),
create_instruction(INSTR_STORE, MODE_REGISTER, EMPTY_BYTE,
0x09),
create_instruction(ADV_INSTR_PRINT, MODE_EXTRA, EMPTY_BYTE,
EMPTY_BYTE),
create_instruction(INSTR_EXIT, EMPTY_BYTE, EMPTY_BYTE,
EMPTY_BYTE),
0x68656C6C, // The string 'hello world\n'
0x6F20776F,
0x726C640A};

// Load it
load_program(program, sizeof(program) / sizeof(program[0]), flash);
Expand Down
13 changes: 7 additions & 6 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void allocate_vm(unsigned int ram_size, unsigned int flash_size,
ram = malloc(ram_size);
flash = malloc(flash_size);
r = malloc(num_registers * register_size);
stack = (uint32_t *)(ram + (ram_size - stack_size)); //+ PROGRAM_SPACE;
stack = (uint32_t *) (ram + (ram_size - stack_size)); //+ PROGRAM_SPACE;
sp = 0;
pc = 0;
sr = 0;
Expand Down Expand Up @@ -96,10 +96,11 @@ int copy_memory(uint8_t *source, uint8_t *destination, int num_dwords)
// Memory is byte addressable but the program image is composed of 4
// byte instructions and 4 byte data so here it is split into bytes
// and copied
*(destination) = (*((uint32_t *)source + i) & 0xFF000000) >> 24;
*(destination + 1) = (*((uint32_t *)source + i) & 0x00FF0000) >> 16;
*(destination + 2) = (*((uint32_t *)source + i) & 0x0000FF00) >> 8;
*(destination + 3) = (*((uint32_t *)source + i) & 0x000000FF);
*(destination) = (*((uint32_t *) source + i) & 0xFF000000) >> 24;
*(destination + 1) =
(*((uint32_t *) source + i) & 0x00FF0000) >> 16;
*(destination + 2) = (*((uint32_t *) source + i) & 0x0000FF00) >> 8;
*(destination + 3) = (*((uint32_t *) source + i) & 0x000000FF);

// Increment destination counter to next 4 byte block
destination += INSTRUCTION_SIZE;
Expand All @@ -125,7 +126,7 @@ void exec_program()
// Print the entire program image
disp_image(flash, flash_allocated);
// Copy the contents of the program to this VM's RAM
if (0 != copy_memory((uint8_t *)flash, ram, flash_allocated))
if (0 != copy_memory((uint8_t *) flash, ram, flash_allocated))
{
printf("\nERROR: Memory copy failed!\n");
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions tests/instructions/test_instructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ void test_instr_exit()
{
// TODO
}

// TODO: Add the rest
Loading

0 comments on commit c30693f

Please sign in to comment.