Skip to content

Commit

Permalink
Add different printing options
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Feb 6, 2020
1 parent 9339344 commit b119c88
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 187 deletions.
36 changes: 36 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
#AllowAllArgumentsOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Allman
ColumnLimit: 100
Cpp11BracedListStyle: false
IncludeBlocks: Regroup
IndentWidth: 4
Language: Cpp
NamespaceIndentation: None
PointerAlignment: Left
SortUsingDeclarations: true
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpacesInCStyleCastParentheses: true
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: true
SpaceBeforeCpp11BracedList: true
Standard: Auto
UseTab: Never

...
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Downloading

Build for Windows and Linux are available [here](https://github.com/hjaremko/von-neumann/releases).
Builds for Windows and Linux are available [here](https://github.com/hjaremko/von-neumann/releases).

### Building
```
Expand All @@ -23,6 +23,8 @@ $ ./von-neumann [OPTION...]
-s, --save Save output to file
-r, --register Print register values before every cycle
-m, --memory Print memory before every cycle
-b, --binary Print instruction arguments in binary
-d, --signed Print instruction arguments as 9-bit signed integers
```
**Example:**
```
Expand Down
8 changes: 3 additions & 5 deletions include/machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class machine
public:
machine() = default;

void print_registers_table( std::ostream& ) const;
void print_registers( std::ostream& ) const;
void print_memory( std::ostream& ) const;
void put_to_memory( const word&, const word& );
void set_pc( const word& );
void set_or( const word& );
Expand All @@ -23,7 +20,8 @@ class machine
[[nodiscard]] memory get_memory() const;
[[nodiscard]] word get_or() const;
[[nodiscard]] word get_ac() const;
[[nodiscard]] int get_size() const;
[[nodiscard]] word get_ir() const;
[[nodiscard]] word get_pc() const;
bool execute();

private:
Expand All @@ -36,7 +34,7 @@ class machine

inline void machine::tick()
{
instruction_reg_ = memory_.get( program_counter_ );
instruction_reg_ = memory_.at( program_counter_ );
++program_counter_;
}

Expand Down
2 changes: 1 addition & 1 deletion include/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace vnm
class memory
{
public:
[[nodiscard]] word get( const word& ) const;
[[nodiscard]] word at( const word& ) const;
void set( const word&, const word& );

private:
Expand Down
225 changes: 225 additions & 0 deletions include/printer_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#ifndef PRINTER_INTERFACE_HPP
#define PRINTER_INTERFACE_HPP

#include <bitset>

// TODO: print all in binary

namespace vnm
{
class printer_interface
{
public:
virtual void print_registers_table() const = 0;
virtual void print_registers() const = 0;
virtual void print_memory() const = 0;
};

class word_printer_interface
{
public:
virtual void print_word( std::ostream&, const vnm::word& w ) const = 0;
};

class word_default_printer : public word_printer_interface
{
public:
void print_word( std::ostream& os_, const word& rhs ) const override
{
std::stringstream ss;

if ( rhs.is_instruction() )
{
if ( rhs.get_code() == vnm::instruction::STOP )
{
ss << "STOP";
}
else
{
ss << std::left << std::setw( 6 ) << instructions_to_str.at( rhs.get_code() )
<< mode_to_str.at( rhs.get_mode() ) << ' ' << *rhs.get_arg();
}
}
else if ( *rhs != 0 )
{
ss << *rhs;
}

os_ << ss.str();
}
};

class default_printer : public printer_interface, public word_default_printer
{
public:
default_printer( std::ostream& os, const machine& machine ) : os_( os ), machine_( machine )
{
}

void print_registers_table() const override
{
os_ << "--------------------------------------------------" << std::endl;
os_ << "| IR | | | | |" << std::endl;
os_ << "----------------------| PC | OR | AC | next |" << std::endl;
os_ << "| code | mode | arg | | | | |" << std::endl;
os_ << "--------------------------------------------------" << std::endl;
}

void print_registers() const override
{
os_ << std::left;
os_ << "| " << std::setw( 6 );

const auto& ir { machine_.get_ir() };
const auto& ir_code { ir.get_code() };
const auto& ir_mode { ir.get_mode() };
const auto& ir_arg { ir.get_arg() };
const auto& pc { machine_.get_pc() };
const auto& oreg { machine_.get_or() };
const auto& ac { machine_.get_ac() };
const auto& next { machine_.get_memory().at( pc ) };

if ( ir.is_instruction() )
os_ << instructions_to_str.at( ir_code );
else
os_ << ' ';

os_ << "| " << std::setw( 4 ) << mode_to_str.at( ir_mode );
os_ << "| " << std::setw( 5 );
print_word( os_, ir_arg );
os_ << "| " << std::setw( 3 ) << *pc;
os_ << "| " << std::setw( 3 ) << *oreg;
os_ << "| " << std::setw( 3 ) << *ac;
os_ << "| " << std::setw( 10 );
print_word( os_, next );
os_ << "|" << std::endl;
os_ << "--------------------------------------------------" << std::endl;
}

void print_memory() const override
{
os_ << "--------------------------------------------------" << std::endl;

word::type i = 0;
// for ( ; machine_.get_memory().get( word { i } ) != vnm::stop; ++i )
for ( ; i < get_size(); ++i )
{
print_memory_cell( i );
}

print_memory_cell( i );

os_ << "--------------------------------------------------" << std::endl;
}

private:
void print_memory_cell( word::type i ) const
{
os_ << "[ " << std::left << std::setw( 3 ) << i << " ]: ";
print_word( os_, machine_.get_memory().at( word { static_cast<word::type>( i ) } ) );
os_ << std::endl;
}

[[nodiscard]] word::type get_size() const
{
for ( word::type i = 511; i >= 0; --i )
{
if ( *machine_.get_memory().at( word { i } ) != 0 )
{
return i + 2;
}
}

return 511;
}

std::ostream& os_;
const machine& machine_;
};

class binary_printer : public default_printer
{
public:
binary_printer( std::ostream& os, const machine& machine ) : default_printer( os, machine )
{
}

void print_word( std::ostream& o, const word& rhs ) const override
{
std::stringstream ss;

if ( rhs.is_instruction() )
{
if ( rhs.get_code() == vnm::instruction::STOP )
{
ss << "STOP";
}
else
{
ss << std::left << std::setw( 6 ) << instructions_to_str.at( rhs.get_code() )
<< mode_to_str.at( rhs.get_mode() ) << ' ';
ss << std::bitset<9>( *rhs.get_arg() );
}
}
else if ( *rhs != 0 )
{
ss << std::bitset<16>( *rhs );
}

o << ss.str();
}
};

class signed_printer : public default_printer
{
public:
signed_printer( std::ostream& os, const machine& machine ) : default_printer( os, machine )
{
}

struct s
{
int val : 9;
};

void print_word( std::ostream& o, const word& rhs ) const override
{
std::stringstream ss;

if ( rhs.is_instruction() )
{
if ( rhs.get_code() == vnm::instruction::STOP )
{
ss << "STOP";
}
else
{
ss << std::left << std::setw( 6 ) << instructions_to_str.at( rhs.get_code() )
<< mode_to_str.at( rhs.get_mode() ) << ' ';
const auto t { s { *rhs.get_arg() } };
ss << t.val;
}
}
else if ( *rhs != 0 )
{
ss << static_cast<int16_t>( *rhs );
}

o << ss.str();
}
};

std::unique_ptr<printer_interface>
make_printer( const cxxopts::ParseResult& parse_result, std::ostream& os, const machine& m )
{
if ( parse_result.count( "binary" ) )
return std::make_unique<binary_printer>( os, m );

if ( parse_result.count( "signed" ) )
return std::make_unique<signed_printer>( os, m );

return std::make_unique<default_printer>( os, m );
}
} // namespace vnm

#endif
Loading

0 comments on commit b119c88

Please sign in to comment.