Skip to content

Commit b119c88

Browse files
author
Hubert Jaremko
committed
Add different printing options
1 parent 9339344 commit b119c88

12 files changed

Lines changed: 380 additions & 187 deletions

File tree

.clang-format

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
AccessModifierOffset: -4
3+
AlignAfterOpenBracket: Align
4+
AlignTrailingComments: true
5+
AllowAllParametersOfDeclarationOnNextLine: true
6+
#AllowAllArgumentsOnNextLine: true
7+
AllowShortBlocksOnASingleLine: false
8+
AllowShortCaseLabelsOnASingleLine: false
9+
AllowShortFunctionsOnASingleLine: None
10+
AllowShortIfStatementsOnASingleLine: Never
11+
AllowShortLoopsOnASingleLine: false
12+
AlwaysBreakAfterDefinitionReturnType: None
13+
AlwaysBreakTemplateDeclarations: Yes
14+
BinPackArguments: false
15+
BinPackParameters: false
16+
BreakBeforeBraces: Allman
17+
ColumnLimit: 100
18+
Cpp11BracedListStyle: false
19+
IncludeBlocks: Regroup
20+
IndentWidth: 4
21+
Language: Cpp
22+
NamespaceIndentation: None
23+
PointerAlignment: Left
24+
SortUsingDeclarations: true
25+
SpaceAfterTemplateKeyword: true
26+
SpaceBeforeAssignmentOperators: true
27+
SpaceBeforeParens: ControlStatements
28+
SpacesInCStyleCastParentheses: true
29+
SpacesInContainerLiterals: true
30+
SpacesInParentheses: true
31+
SpacesInSquareBrackets: true
32+
SpaceBeforeCpp11BracedList: true
33+
Standard: Auto
34+
UseTab: Never
35+
36+
...

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Downloading
44

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

77
### Building
88
```
@@ -23,6 +23,8 @@ $ ./von-neumann [OPTION...]
2323
-s, --save Save output to file
2424
-r, --register Print register values before every cycle
2525
-m, --memory Print memory before every cycle
26+
-b, --binary Print instruction arguments in binary
27+
-d, --signed Print instruction arguments as 9-bit signed integers
2628
```
2729
**Example:**
2830
```

include/machine.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class machine
1111
public:
1212
machine() = default;
1313

14-
void print_registers_table( std::ostream& ) const;
15-
void print_registers( std::ostream& ) const;
16-
void print_memory( std::ostream& ) const;
1714
void put_to_memory( const word&, const word& );
1815
void set_pc( const word& );
1916
void set_or( const word& );
@@ -23,7 +20,8 @@ class machine
2320
[[nodiscard]] memory get_memory() const;
2421
[[nodiscard]] word get_or() const;
2522
[[nodiscard]] word get_ac() const;
26-
[[nodiscard]] int get_size() const;
23+
[[nodiscard]] word get_ir() const;
24+
[[nodiscard]] word get_pc() const;
2725
bool execute();
2826

2927
private:
@@ -36,7 +34,7 @@ class machine
3634

3735
inline void machine::tick()
3836
{
39-
instruction_reg_ = memory_.get( program_counter_ );
37+
instruction_reg_ = memory_.at( program_counter_ );
4038
++program_counter_;
4139
}
4240

include/memory.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace vnm
1111
class memory
1212
{
1313
public:
14-
[[nodiscard]] word get( const word& ) const;
14+
[[nodiscard]] word at( const word& ) const;
1515
void set( const word&, const word& );
1616

1717
private:

include/printer_interface.hpp

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#ifndef PRINTER_INTERFACE_HPP
2+
#define PRINTER_INTERFACE_HPP
3+
4+
#include <bitset>
5+
6+
// TODO: print all in binary
7+
8+
namespace vnm
9+
{
10+
class printer_interface
11+
{
12+
public:
13+
virtual void print_registers_table() const = 0;
14+
virtual void print_registers() const = 0;
15+
virtual void print_memory() const = 0;
16+
};
17+
18+
class word_printer_interface
19+
{
20+
public:
21+
virtual void print_word( std::ostream&, const vnm::word& w ) const = 0;
22+
};
23+
24+
class word_default_printer : public word_printer_interface
25+
{
26+
public:
27+
void print_word( std::ostream& os_, const word& rhs ) const override
28+
{
29+
std::stringstream ss;
30+
31+
if ( rhs.is_instruction() )
32+
{
33+
if ( rhs.get_code() == vnm::instruction::STOP )
34+
{
35+
ss << "STOP";
36+
}
37+
else
38+
{
39+
ss << std::left << std::setw( 6 ) << instructions_to_str.at( rhs.get_code() )
40+
<< mode_to_str.at( rhs.get_mode() ) << ' ' << *rhs.get_arg();
41+
}
42+
}
43+
else if ( *rhs != 0 )
44+
{
45+
ss << *rhs;
46+
}
47+
48+
os_ << ss.str();
49+
}
50+
};
51+
52+
class default_printer : public printer_interface, public word_default_printer
53+
{
54+
public:
55+
default_printer( std::ostream& os, const machine& machine ) : os_( os ), machine_( machine )
56+
{
57+
}
58+
59+
void print_registers_table() const override
60+
{
61+
os_ << "--------------------------------------------------" << std::endl;
62+
os_ << "| IR | | | | |" << std::endl;
63+
os_ << "----------------------| PC | OR | AC | next |" << std::endl;
64+
os_ << "| code | mode | arg | | | | |" << std::endl;
65+
os_ << "--------------------------------------------------" << std::endl;
66+
}
67+
68+
void print_registers() const override
69+
{
70+
os_ << std::left;
71+
os_ << "| " << std::setw( 6 );
72+
73+
const auto& ir { machine_.get_ir() };
74+
const auto& ir_code { ir.get_code() };
75+
const auto& ir_mode { ir.get_mode() };
76+
const auto& ir_arg { ir.get_arg() };
77+
const auto& pc { machine_.get_pc() };
78+
const auto& oreg { machine_.get_or() };
79+
const auto& ac { machine_.get_ac() };
80+
const auto& next { machine_.get_memory().at( pc ) };
81+
82+
if ( ir.is_instruction() )
83+
os_ << instructions_to_str.at( ir_code );
84+
else
85+
os_ << ' ';
86+
87+
os_ << "| " << std::setw( 4 ) << mode_to_str.at( ir_mode );
88+
os_ << "| " << std::setw( 5 );
89+
print_word( os_, ir_arg );
90+
os_ << "| " << std::setw( 3 ) << *pc;
91+
os_ << "| " << std::setw( 3 ) << *oreg;
92+
os_ << "| " << std::setw( 3 ) << *ac;
93+
os_ << "| " << std::setw( 10 );
94+
print_word( os_, next );
95+
os_ << "|" << std::endl;
96+
os_ << "--------------------------------------------------" << std::endl;
97+
}
98+
99+
void print_memory() const override
100+
{
101+
os_ << "--------------------------------------------------" << std::endl;
102+
103+
word::type i = 0;
104+
// for ( ; machine_.get_memory().get( word { i } ) != vnm::stop; ++i )
105+
for ( ; i < get_size(); ++i )
106+
{
107+
print_memory_cell( i );
108+
}
109+
110+
print_memory_cell( i );
111+
112+
os_ << "--------------------------------------------------" << std::endl;
113+
}
114+
115+
private:
116+
void print_memory_cell( word::type i ) const
117+
{
118+
os_ << "[ " << std::left << std::setw( 3 ) << i << " ]: ";
119+
print_word( os_, machine_.get_memory().at( word { static_cast<word::type>( i ) } ) );
120+
os_ << std::endl;
121+
}
122+
123+
[[nodiscard]] word::type get_size() const
124+
{
125+
for ( word::type i = 511; i >= 0; --i )
126+
{
127+
if ( *machine_.get_memory().at( word { i } ) != 0 )
128+
{
129+
return i + 2;
130+
}
131+
}
132+
133+
return 511;
134+
}
135+
136+
std::ostream& os_;
137+
const machine& machine_;
138+
};
139+
140+
class binary_printer : public default_printer
141+
{
142+
public:
143+
binary_printer( std::ostream& os, const machine& machine ) : default_printer( os, machine )
144+
{
145+
}
146+
147+
void print_word( std::ostream& o, const word& rhs ) const override
148+
{
149+
std::stringstream ss;
150+
151+
if ( rhs.is_instruction() )
152+
{
153+
if ( rhs.get_code() == vnm::instruction::STOP )
154+
{
155+
ss << "STOP";
156+
}
157+
else
158+
{
159+
ss << std::left << std::setw( 6 ) << instructions_to_str.at( rhs.get_code() )
160+
<< mode_to_str.at( rhs.get_mode() ) << ' ';
161+
ss << std::bitset<9>( *rhs.get_arg() );
162+
}
163+
}
164+
else if ( *rhs != 0 )
165+
{
166+
ss << std::bitset<16>( *rhs );
167+
}
168+
169+
o << ss.str();
170+
}
171+
};
172+
173+
class signed_printer : public default_printer
174+
{
175+
public:
176+
signed_printer( std::ostream& os, const machine& machine ) : default_printer( os, machine )
177+
{
178+
}
179+
180+
struct s
181+
{
182+
int val : 9;
183+
};
184+
185+
void print_word( std::ostream& o, const word& rhs ) const override
186+
{
187+
std::stringstream ss;
188+
189+
if ( rhs.is_instruction() )
190+
{
191+
if ( rhs.get_code() == vnm::instruction::STOP )
192+
{
193+
ss << "STOP";
194+
}
195+
else
196+
{
197+
ss << std::left << std::setw( 6 ) << instructions_to_str.at( rhs.get_code() )
198+
<< mode_to_str.at( rhs.get_mode() ) << ' ';
199+
const auto t { s { *rhs.get_arg() } };
200+
ss << t.val;
201+
}
202+
}
203+
else if ( *rhs != 0 )
204+
{
205+
ss << static_cast<int16_t>( *rhs );
206+
}
207+
208+
o << ss.str();
209+
}
210+
};
211+
212+
std::unique_ptr<printer_interface>
213+
make_printer( const cxxopts::ParseResult& parse_result, std::ostream& os, const machine& m )
214+
{
215+
if ( parse_result.count( "binary" ) )
216+
return std::make_unique<binary_printer>( os, m );
217+
218+
if ( parse_result.count( "signed" ) )
219+
return std::make_unique<signed_printer>( os, m );
220+
221+
return std::make_unique<default_printer>( os, m );
222+
}
223+
} // namespace vnm
224+
225+
#endif

0 commit comments

Comments
 (0)