From 3c6f886d5a8bfd36c796b963d6e3178ad9577742 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 13 May 2009 16:55:17 +0200 Subject: * added documentation (no more TODOs) * added testsuite + testcase * used copyctor instead of assign operator more often --- .hgignore | 1 + ue3/mycpu/ccpu.cpp | 3 ++- ue3/mycpu/ccpu.h | 6 +++-- ue3/mycpu/cdisplay.h | 9 ++++--- ue3/mycpu/cinstruction.cpp | 4 ++-- ue3/mycpu/cinstruction.h | 6 ++--- ue3/mycpu/cprogram.cpp | 6 ++--- ue3/mycpu/cprogram.h | 5 ++-- ue3/mycpu/displays.h | 8 ++++--- ue3/mycpu/instructions.cpp | 6 ++--- ue3/mycpu/instructions.h | 57 ++++++++++++++++++++++++++++++++------------ ue3/mycpu/mycpu.cpp | 25 ++++++++++++------- ue3/mycpu/test/test.sh | 40 +++++++++++++++++++++++++++++++ ue3/mycpu/test/test1_memory | 1 + ue3/mycpu/test/test1_output | 19 +++++++++++++++ ue3/mycpu/test/test1_program | 13 ++++++++++ 16 files changed, 161 insertions(+), 48 deletions(-) create mode 100755 ue3/mycpu/test/test.sh create mode 100644 ue3/mycpu/test/test1_memory create mode 100644 ue3/mycpu/test/test1_output create mode 100644 ue3/mycpu/test/test1_program diff --git a/.hgignore b/.hgignore index 2dd5fef..b96a5d0 100644 --- a/.hgignore +++ b/.hgignore @@ -5,6 +5,7 @@ syntax: glob *.so *.swp *~ +tmpfile ue1/imgsynth/imgsynth ue2/imgsynth/test/*_out* ue2/imgsynth2/imgsynth2 diff --git a/ue3/mycpu/ccpu.cpp b/ue3/mycpu/ccpu.cpp index 16209e2..af86200 100644 --- a/ue3/mycpu/ccpu.cpp +++ b/ue3/mycpu/ccpu.cpp @@ -1,7 +1,8 @@ /** * @module ccpu * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief CPU implementation. Used as a container for memory and instructions. + * Implements an run method to execute the program (= the instructions). * @date 10.05.2009 */ diff --git a/ue3/mycpu/ccpu.h b/ue3/mycpu/ccpu.h index 01c897f..05b5c03 100644 --- a/ue3/mycpu/ccpu.h +++ b/ue3/mycpu/ccpu.h @@ -1,7 +1,8 @@ /** * @module ccpu * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief CPU implementation. Used as a container for memory and instructions. + * Implements a run method to execute the program (= the instructions). * @date 10.05.2009 */ @@ -18,7 +19,8 @@ /** * @class CCPU * - * TODO + * CPU implementation. Used as a container for memory and instructions. + * Implements a run method to execute the program (= the instructions). */ class CCPU { diff --git a/ue3/mycpu/cdisplay.h b/ue3/mycpu/cdisplay.h index ed9b84d..c2a84a6 100644 --- a/ue3/mycpu/cdisplay.h +++ b/ue3/mycpu/cdisplay.h @@ -1,7 +1,7 @@ /** * @module cdisplay * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief Abstract class for displays * @date 10.05.2009 */ @@ -11,7 +11,7 @@ /** * @class CDisplay * - * TODO + * Abstract class for displays */ class CDisplay { @@ -26,9 +26,8 @@ class CDisplay * @conditions none */ CDisplay(std::string name) - { - m_name = name; - } + : m_name(name) + {} /** * @method ~CDisplay diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp index 57acd5f..5c1bd5c 100644 --- a/ue3/mycpu/cinstruction.cpp +++ b/ue3/mycpu/cinstruction.cpp @@ -1,8 +1,8 @@ /** * @module cinstruction * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO - * @date 10.05.2009 + * @brief Abstract class for displays + * @date 13.05.2009 */ #include diff --git a/ue3/mycpu/cinstruction.h b/ue3/mycpu/cinstruction.h index 40e9ddc..942d8cf 100644 --- a/ue3/mycpu/cinstruction.h +++ b/ue3/mycpu/cinstruction.h @@ -1,8 +1,8 @@ /** * @module cinstruction * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO - * @date 10.05.2009 + * @brief Abstract class for displays + * @date 13.05.2009 */ #ifndef CINSTRUCTION_H @@ -17,7 +17,7 @@ class CCPU; /** * @class CInstruction * - * TODO + * Abstract class for displays */ class CInstruction { diff --git a/ue3/mycpu/cprogram.cpp b/ue3/mycpu/cprogram.cpp index f904bce..1a450f5 100644 --- a/ue3/mycpu/cprogram.cpp +++ b/ue3/mycpu/cprogram.cpp @@ -1,7 +1,7 @@ /** * @module cprogram * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief CProgram extends std::vector and adds a method for parsing programfile * @date 12.05.2009 */ @@ -75,7 +75,7 @@ void CProgram::compile(std::istream& in) /* get instruction name */ size_t pos = line.find_first_of(' '); - string instrname = line.substr(0, pos); + string instrname(line.substr(0, pos)); /* search and create instruction */ CInstruction *instrptr = NULL; @@ -112,7 +112,7 @@ void CProgram::compile(std::istream& in) { if (instrparams.size() != 1) throw runtime_error("Invalid paramater count - must be 1"); - string label = instrparams.front(); + string label(instrparams.front()); if (label.length() < 2 || label[ label.length() - 1] != ':') throw runtime_error("Label has invalid syntax"); m_labels[ label.substr(0, label.length() - 1) ] = size(); diff --git a/ue3/mycpu/cprogram.h b/ue3/mycpu/cprogram.h index c145832..27e7647 100644 --- a/ue3/mycpu/cprogram.h +++ b/ue3/mycpu/cprogram.h @@ -1,7 +1,7 @@ /** * @module cprogram * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief CProgram extends std::vector and adds a method for parsing programfile * @date 10.05.2009 */ @@ -16,7 +16,8 @@ /** * @class CProgram * - * TODO + * CProgram extends std::vector and adds a method for parsing + * programfile. This adds instances of CInstruction to CProgram itself. */ class CProgram : public std::vector diff --git a/ue3/mycpu/displays.h b/ue3/mycpu/displays.h index f7adbdb..87b9408 100644 --- a/ue3/mycpu/displays.h +++ b/ue3/mycpu/displays.h @@ -1,7 +1,7 @@ /** * @module displays * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief Implementations of CDisplay * @date 10.05.2009 */ @@ -14,7 +14,8 @@ /** * @class CDisplayWDEZ * - * TODO + * Implementation of CDisplay + * Prints CDat to stdout as decimal */ class CDisplayWDEZ : public CDisplay @@ -35,7 +36,8 @@ class CDisplayWDEZ /** * @class CDisplayWHEX * - * TODO + * Implementation of CDisplay + * Prints CDat to stdout as decimal */ class CDisplayWHEX : public CDisplay diff --git a/ue3/mycpu/instructions.cpp b/ue3/mycpu/instructions.cpp index a6ac611..c2ce096 100644 --- a/ue3/mycpu/instructions.cpp +++ b/ue3/mycpu/instructions.cpp @@ -1,7 +1,7 @@ /** * @module instructions * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief Implementations of CInstruction * @date 10.05.2009 */ @@ -162,7 +162,7 @@ void CInstructionLoad::execute(CCPU *cpu) { checkRegister(cpu, m_regidx1); checkRegister(cpu, m_regidx2); - CDat val = cpu->getRegisters()[ m_regidx2 ]; + CDat val(cpu->getRegisters()[ m_regidx2 ]); cpu->getRegisters()[ m_regidx1 ] = (*cpu->getMemory())[ val ]; } @@ -184,7 +184,7 @@ void CInstructionStore::execute(CCPU *cpu) { checkRegister(cpu, m_regidx1); checkRegister(cpu, m_regidx2); - CDat val = cpu->getRegisters()[ m_regidx2 ]; + CDat val(cpu->getRegisters()[ m_regidx2 ]); (*cpu->getMemory())[ val ] = cpu->getRegisters()[ m_regidx1 ]; } diff --git a/ue3/mycpu/instructions.h b/ue3/mycpu/instructions.h index 0e4d99c..a52b991 100644 --- a/ue3/mycpu/instructions.h +++ b/ue3/mycpu/instructions.h @@ -1,7 +1,7 @@ /** * @module instructions * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO + * @brief Implementations of CInstruction * @date 10.05.2009 */ @@ -14,7 +14,9 @@ /** * @class CInstructionInc * - * TODO + * Implementation of assembler command "inc" + * Syntax: inc R1 + * (R1++) */ class CInstructionInc : public CInstruction @@ -41,7 +43,9 @@ class CInstructionInc /** * @class CInstructionDec * - * TODO + * Implementation of assembler command "dec" + * Syntax: dec R1 + * (R1--) */ class CInstructionDec : public CInstruction @@ -68,7 +72,9 @@ class CInstructionDec /** * @class CInstructionAdd * - * TODO + * Implementation of assembler command "add" + * Syntax: add R1, R2, R3 + * (R1 = R2 + R3) */ class CInstructionAdd : public CInstruction @@ -97,7 +103,9 @@ class CInstructionAdd /** * @class CInstructionSub * - * TODO + * Implementation of assembler command "sub" + * Syntax: sub R1, R2, R3 + * (R1 = R2 - R3) */ class CInstructionSub : public CInstruction @@ -126,7 +134,9 @@ class CInstructionSub /** * @class CInstructionMul * - * TODO + * Implementation of assembler command "mul" + * Syntax: mul R1, R2, R3 + * (R1 = R2 * R3) */ class CInstructionMul : public CInstruction @@ -155,7 +165,9 @@ class CInstructionMul /** * @class CInstructionDiv * - * TODO + * Implementation of assembler command "div" + * Syntax: div R1, R2, R3 + * (R1 = R2 / R3) */ class CInstructionDiv : public CInstruction @@ -184,7 +196,9 @@ class CInstructionDiv /** * @class CInstructionLoad * - * TODO + * Implementation of assembler command "load" + * Syntax: load R1, R2 + * (R1 = memory[R2]) */ class CInstructionLoad : public CInstruction @@ -212,7 +226,9 @@ class CInstructionLoad /** * @class CInstructionStore * - * TODO + * Implementation of assembler command "store" + * Syntax: store R1, R2 + * (memory[R2] = R1) */ class CInstructionStore : public CInstruction @@ -240,7 +256,9 @@ class CInstructionStore /** * @class CInstructionTest * - * TODO + * Implementation of assembler command "test" + * Syntax: test R1 + * (R1 == 0: zeroflag: true, R1 < 0: signflag: true) */ class CInstructionTest : public CInstruction @@ -267,7 +285,8 @@ class CInstructionTest /** * @class CInstructionLabel * - * TODO + * Implementation of assembler command "label" + * Syntax: label name: */ class CInstructionLabel : public CInstruction @@ -294,7 +313,9 @@ class CInstructionLabel /** * @class CInstructionJumpA * - * TODO + * Implementation of assembler command "jumpa" + * Syntax: jumpa labelname + * (jump to labelname) */ class CInstructionJumpA : public CInstruction @@ -321,7 +342,9 @@ class CInstructionJumpA /** * @class CInstructionJumpZ * - * TODO + * Implementation of assembler command "jumpz" + * Syntax: jumpz labelname + * (jump to labelname if zeroflag) */ class CInstructionJumpZ : public CInstruction @@ -348,7 +371,9 @@ class CInstructionJumpZ /** * @class CInstructionJumpS * - * TODO + * Implementation of assembler command "jumps" + * Syntax: jumps labelname + * (jump to labelname if signflag) */ class CInstructionJumpS : public CInstruction @@ -375,7 +400,9 @@ class CInstructionJumpS /** * @class CInstructionWrite * - * TODO + * Implementation of assembler command "write" + * Syntax: write DEV, R1 + * (write R1 to DEV, which is a name of a display) */ class CInstructionWrite : public CInstruction diff --git a/ue3/mycpu/mycpu.cpp b/ue3/mycpu/mycpu.cpp index 08861a4..b25e721 100644 --- a/ue3/mycpu/mycpu.cpp +++ b/ue3/mycpu/mycpu.cpp @@ -1,10 +1,14 @@ /** * @module mycpu * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief TODO - * @date 11.05.2009 + * @brief mycpu executes a programfile (in simple assembler) by parsing the + * programfile first. This creates a vector of instructions, which will + * be executed in linear order (except jumps) afterwards. In order to + * initialize the memory of the cpu before execution an optional + * memoryfile can be passed as commandline option. + * @date 13.05.2009 * @par Exercise - * 3 + * 4 */ #include @@ -29,7 +33,11 @@ namespace po = boost::program_options; * @exception none * @conditions none * - * TODO + * parse commandline options, create and initialize memory, + * create cprogram instance, which parses the programfile and + * execute CCPU::run() + * On error print error message to stderr. + * Unknown commandline options will print a usage message. */ int main(int argc, char* argv[]) { @@ -88,7 +96,7 @@ int main(int argc, char* argv[]) } #if DEBUG - memory.dump(cout); + memory.dump(cerr); #endif } @@ -116,7 +124,7 @@ int main(int argc, char* argv[]) } #if DEBUG - program.dump(cout); + program.dump(cerr); #endif @@ -128,7 +136,7 @@ int main(int argc, char* argv[]) cpu.setProgram(&program); cpu.run(); #if DEBUG - //cpu.dumpRegisters(cout); + //cpu.dumpRegisters(cerr); #endif } catch(runtime_error& ex) @@ -136,8 +144,7 @@ int main(int argc, char* argv[]) cerr << me << ": Error while executing program:" << endl << " " << ex.what() << endl; #if DEBUG - memory.dump(cout); - //cpu.dumpRegisters(cout); + memory.dump(cerr); #endif return 1; } diff --git a/ue3/mycpu/test/test.sh b/ue3/mycpu/test/test.sh new file mode 100755 index 0000000..ad2ae4d --- /dev/null +++ b/ue3/mycpu/test/test.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +binary="./mycpu" +tmpfile="test/tmpfile" +inputs=( $(ls test/* | grep _program | sort -n) ) + +for input in ${inputs[@]} +do + echo "Testing $input ..." + + programfile="$input" + args="-c $programfile" + memoryfile="${input/_program/_memory}" + reffile="${input/_program/_output}" + if [ -e "$memoryfile" ] + then + args+=" -m $memoryfile" + fi + + if [ ! -e "$reffile" ] + then + echo " ERROR: reference file $reffile doesn't exist" + exit 1 + fi + + rm -rf "$tmpfile" + echo " Executing $binary $args ..." + $binary $args > $tmpfile + + md5_1=$(md5sum < "$reffile") + md5_2=$(md5sum < "$tmpfile") + if [ "$md5_1" != "$md5_2" ] + then + echo " ERROR: output and $reffile differ" + diff -Nau $reffile $tmpfile + exit 1 + else + echo " SUCCESS" + fi +done diff --git a/ue3/mycpu/test/test1_memory b/ue3/mycpu/test/test1_memory new file mode 100644 index 0000000..209e3ef --- /dev/null +++ b/ue3/mycpu/test/test1_memory @@ -0,0 +1 @@ +20 diff --git a/ue3/mycpu/test/test1_output b/ue3/mycpu/test/test1_output new file mode 100644 index 0000000..ac30dc2 --- /dev/null +++ b/ue3/mycpu/test/test1_output @@ -0,0 +1,19 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 diff --git a/ue3/mycpu/test/test1_program b/ue3/mycpu/test/test1_program new file mode 100644 index 0000000..ae5e9d2 --- /dev/null +++ b/ue3/mycpu/test/test1_program @@ -0,0 +1,13 @@ +# set R2 = 10 +LOAD R2, R1 + +# start of loop +label Loop: +inc R3 +sub R4, R3, R2 +test R4 +jumpz EndLoop +write WDEZ, R3 +jumpa Loop + +label EndLoop: -- cgit v1.2.3