Skip to content
Snippets Groups Projects
Commit dccbec20 authored by iftach's avatar iftach
Browse files

implemented test functions along with some tests

parent 387a10dd
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ struct ProcessorState
Remember to check memory's bounds before accessing it!
Use uint32_t to represent data/instructions/register values
Use int to represent booleans and anything that doesn't need more than 16 bits (apparently ints can be only 16 bits on some systems)
Use bool to represent booleans and int (if you want) to represent anything that doesn't need more than 16 bits (apparently ints can be only 16 bits on some systems)
NON-HELPER FUNCTIONS
......@@ -32,12 +32,12 @@ void step(struct ProcessorState *processor)
uses: dataProcessingInstr(...), multiplyInstr(...), dataTransferInstr(...), branchInstr(...)
void dataProcessingInstr(struct ProcessorState *processor, int address)
Executes a data processing instruction stored in (*processor).memory[address]
Executes a data processing instruction stored in processor->memory[address]
uses: isValidCond(...), barrelShift(...), applyOperation(...)
int32_t applyOperation(int opcode, int32_t operand1, int32_t operand2, int *carry, int *store)
Applies a binary operation (from an opcode) on two operators. Returns the result, and sets the supplied carry bit if it makes sense to do so.
Doesn't change the carry bit when performing a logical operation.
IMPLEMENTED
IMPLEMENTED, needs tests
void multiplyInstr(struct ProcessorState *processor, int address)
......@@ -63,12 +63,12 @@ HELPER FUNCTIONS (probably used in more than one method):
uint32_t getBits(uint32_t source, int start, int end)
Reads bits from source in the range [start, end] (inclusive). Returns a uint32_t with the result as its least significant bits.
(e.g. getBits(8, 2, 3) == 2)
IMPLEMENTED
(e.g. getBits(8, 2, 3) == 2 because 8 == 0000'10'00)
IMPLEMENTED, TESTED
int isSet(uint32_t source, int index)
Shorthand for getBits(source, index, index)
IMPLEMENTED
IMPLEMENTED, TESTED
int validCond(uint32_t cond, struct ProcessorState *processor)
Returns 1 if the condition code 'cond' is satisfied by the processor's current state, and 0 otherwise.
......@@ -76,12 +76,12 @@ int validCond(uint32_t cond, struct ProcessorState *processor)
uint32_t barrelShift(int immediate, uint32_t operand, int *carry, struct ProcessorState *processor)
Implements the functionality of the barrel shifter. The carry output is written to (*carry), just in case you need it.
NOTE: for some reason the I bit is used in opposite ways for data processing instructions vs for data transfer instructions. Arbitrarily, let this method
assume that the data processing instruction interpretation is the right one (so for data transfer instructions just call this method with NOT i).
IMPLEMENTED
NOTE: for some reason the I bit is used in opposite ways for data processing instructions vs for data transfer instructions. Arbitrarily, this method
uses the data processing instruction interpretation (so for data transfer instructions just call this method with !I).
IMPLEMENTED, TESTED
struct ProcessorState *makeProcessor(void)
Creates a new processor struct
Creates a new processor struct. Dynamically allocated, so don't forget to use free() at the end!
IMPLEMENTED
(optionally)
......@@ -91,5 +91,24 @@ int32_t *accessMemory(struct ProcessorState *processor, int address)
Put other stuff you've implemented here!
TESTING STUFF:
void test(bool pass, char *testname)
prints out a formatted message stating the test name and whether it passed.
IMPLEMENTED
uint32_t bin(char* number)
converts a string representing a binary number (e.g. "01001") to a number. For testing purposes.
Ignores spaces and leading zeros, so you can format it like the instructions ("0001 0 00 1 0000").
Doesn't actually check the input at all, so only use it for testing.
IMPLEMENTED
implemented tests:
void testBitGetters(void)
void testBarrelShifter(void)
tests still needed:
applyOperation (and maybe validCond, but it's quite a simple function so idk)
Put other stuff you've implemented here!
No preview for this file type
......@@ -257,10 +257,12 @@ int32_t applyOperation(int opcode, int32_t operand1, int32_t operand2, bool *car
};
}
/* testing helper function (very similar to the one in the slides) */
void testCond(bool ok, char *testname)
//*** TESTING FUNCTIONS ***//
/* testing helper function (similar to the one in the slides) */
void test(bool pass, char *testname)
{
printf("T %s: %s\n", testname, ok ? "\tOK" : "\tFAIL");
printf("T %s: \t%s\n", testname, pass ? "OK" : "FAIL");
}
/* converts string representing a binary number to a number.
......@@ -279,17 +281,17 @@ uint32_t bin(char* number){
return result;
}
void testBitGetters(){
void testBitGetters(void){
printf("*** getBits() tests ***\n");
testCond(getBits(bin("11000000000000000000000101101110"), 28, 31) == 12, "leftmost bits");
testCond(getBits(bin("11000000000000000000000101101110"), 8, 20) == 1, "middle bits");
testCond(getBits(bin("11000000000000000000000101101110"), 0, 3) == 14, "rightmost bits");
test(getBits(bin("11000000000000000000000101101110"), 28, 31) == 12, "leftmost bits");
test(getBits(bin("11000000000000000000000101101110"), 8, 20) == 1, "middle bits");
test(getBits(bin("11000000000000000000000101101110"), 0, 3) == 14, "rightmost bits");
printf("\n");
printf("*** isSet() tests ***\n");
testCond(isSet(bin("11000000000000000000000101101110"), 31), "leftmost bit");
testCond(isSet(bin("11000000000000000000000101101110"), 8), "middle bit");
testCond(!isSet(bin("11000000000000000000000101101110"), 0), "rightmost bit");
test(isSet(bin("11000000000000000000000101101110"), 31), "leftmost bit");
test(isSet(bin("11000000000000000000000101101110"), 8), "middle bit");
test(!isSet(bin("11000000000000000000000101101110"), 0), "rightmost bit");
printf("\n");
}
......@@ -303,27 +305,27 @@ struct ProcessorState *makeProcessor(void){
return processor;
}
void testBarrelShifter(){
void testBarrelShifter(void){
printf("*** barrelShifter() tests ***\n");
struct ProcessorState *processor = makeProcessor();
processor->registers[0] = bin("10110100000000000000000000000000");
processor->registers[1] = 60;
bool carry;
testCond(barrelShift(true, bin("0001 10110100"), &carry, processor) == bin("00101101") && !carry, "Immediate rotation (small)");
testCond(barrelShift(true, bin("0100 10110100"), &carry, processor) == bin("10110100000000000000000000000000") && carry, "Immediate rotation (large)");
test(barrelShift(true, bin("0001 10110100"), &carry, processor) == bin("00101101") && !carry, "Immediate rotation (small)");
test(barrelShift(true, bin("0100 10110100"), &carry, processor) == bin("10110100000000000000000000000000") && carry, "Immediate rotation (large)");
printf("\n");
testCond(barrelShift(false, bin("0001 0 00 1 0000"), &carry, processor) == 0 && !carry, "Register value, register shift count (lsl)");
testCond(barrelShift(false, bin("0001 0 01 1 0000"), &carry, processor) == 0 && !carry, "Register value, register shift count (lsr)");
testCond(barrelShift(false, bin("0001 0 10 1 0000"), &carry, processor) == bin("11111111111111111111111111111111") && carry, "Register value, register shift count (asr)");
testCond(barrelShift(false, bin("0001 0 11 1 0000"), &carry, processor) == bin("01000000000000000000000000001011") && !carry, "Register value, register shift count (ror)");
test(barrelShift(false, bin("0001 0 00 1 0000"), &carry, processor) == 0 && !carry, "Register value, register shift count (lsl)");
test(barrelShift(false, bin("0001 0 01 1 0000"), &carry, processor) == 0 && !carry, "Register value, register shift count (lsr)");
test(barrelShift(false, bin("0001 0 10 1 0000"), &carry, processor) == bin("11111111111111111111111111111111") && carry, "Register value, register shift count (asr)");
test(barrelShift(false, bin("0001 0 11 1 0000"), &carry, processor) == bin("01000000000000000000000000001011") && !carry, "Register value, register shift count (ror)");
printf("\n");
testCond(barrelShift(false, bin("00100 00 0 0000"), &carry, processor) == bin("01000000000000000000000000000000") && carry, "Register value, constant shift count (lsl)");
testCond(barrelShift(false, bin("00100 01 0 0000"), &carry, processor) == bin("00001011010000000000000000000000") && !carry, "Register value, constant shift count (lsr)");
testCond(barrelShift(false, bin("00100 10 0 0000"), &carry, processor) == bin("11111011010000000000000000000000") && !carry, "Register value, constant shift count (asr)");
testCond(barrelShift(false, bin("00100 11 0 0000"), &carry, processor) == bin("00001011010000000000000000000000") && !carry, "Register value, constant shift count (ror)");
test(barrelShift(false, bin("00100 00 0 0000"), &carry, processor) == bin("01000000000000000000000000000000") && carry, "Register value, constant shift count (lsl)");
test(barrelShift(false, bin("00100 01 0 0000"), &carry, processor) == bin("00001011010000000000000000000000") && !carry, "Register value, constant shift count (lsr)");
test(barrelShift(false, bin("00100 10 0 0000"), &carry, processor) == bin("11111011010000000000000000000000") && !carry, "Register value, constant shift count (asr)");
test(barrelShift(false, bin("00100 11 0 0000"), &carry, processor) == bin("00001011010000000000000000000000") && !carry, "Register value, constant shift count (ror)");
printf("\n");
free(processor);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment