|
| 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