51 i_add(
unsigned int result,
unsigned int a,
unsigned int b) {
52 assert(result < nb_registers);
53 assert(a < nb_registers);
54 assert(b < nb_registers);
56 registers[result] = registers[a] + registers[b];
65 assert(result < nb_registers);
66 assert(a < nb_registers);
67 assert(immed6 <= 0x3F);
69 registers[result] = registers[a] + immed6;
77 i_jalr(
unsigned int result,
unsigned int a) {
78 assert(result < nb_registers);
79 assert(a < nb_registers);
81 std::cout <<
"i_jalr() and p_reset() are NOT implemented!";
92 assert(result < nb_registers);
93 assert(immed10 <= 0x3FF);
95 registers[result] = (immed10 << 6);
104 assert(result < nb_registers);
105 assert(a < nb_registers);
106 assert(immed6 <= 0x3F);
108 const unsigned int i = registers[a] + immed6;
110 assert(i < size_memory);
112 registers[result] = memory[i];
113 after_last_memory_acceded = std::max(after_last_memory_acceded, i + 1);
120 i_nand(
unsigned int result,
unsigned int a,
unsigned int b) {
121 assert(result < nb_registers);
122 assert(a < nb_registers);
123 assert(b < nb_registers);
125 registers[result] = ~(registers[a] & registers[b]);
134 assert(result < nb_registers);
135 assert(a < nb_registers);
136 assert(immed6 <= 0x3F);
138 const unsigned int i = registers[result] + immed6;
140 assert(i < size_memory);
142 memory[i] = registers[a];
143 after_last_memory_acceded = std::max(after_last_memory_acceded, i + 1);
167 assert(result < nb_registers);
169 i_lui(result, immed >> 6);
170 i_addi(result, result, immed & 0x3F);
195 after_last_memory_acceded = 0;
216 std::cout <<
"M[" << i <<
"] = ";
221 assert(a < nb_registers);
223 std::cout <<
"R[" << a <<
"] = ";
230 assert(a2 < nb_registers);
231 assert(a1 < nb_registers);
233 std::cout <<
"R[" << a2 <<
':' << a1 <<
"] = ";
241 std::cout <<
"0x" << std::setw(4) << std::hex << n
242 <<
" = 0b" << std::bitset<16>(n)
243 <<
" = " << std::setw(5) << std::dec << n
244 <<
" = " << std::setw(6) << std::dec
245 <<
static_cast<std::int16_t
>(n);
251 std::cout <<
"0x" << std::setw(8) << std::hex << n
252 <<
" = 0b" << std::bitset<32>(n)
253 <<
" = " << std::setw(9) << std::dec << n
254 <<
" = " << std::setw(10) << std::dec
255 <<
static_cast<std::int32_t
>(n);
263 if (after_last_memory_acceded > 0) {
264 std::cout << std::endl;
272 std::cout <<
"# instructions executed = " << nb_executed << std::endl;
279 std::cout << std::endl;
285 for (
unsigned int i = 0; i < (size == 0
295 assert(a < nb_registers);
298 std::cout << std::endl;
304 assert(a2 < nb_registers);
305 assert(a1 < nb_registers);
308 std::cout << std::endl;
323 std::cout << std::endl;
330 std::cout << std::endl;
void p_movi(unsigned int result, immed_t immed)
(MOV Immediate) R[result] <– immed
void i_jalr(unsigned int result, unsigned int a)
(Jump And Link using Register) In the real RiSC16: R[result] <– PC + 1 (where PC = Program Counter)...
void println_memory(unsigned int size)
Print memory items.
void clear_memory()
Reset to 0 all memory items and mark them as not used.
void print_value32(std::uint32_t n)
Print to stdout the 32 bits value n: hexadecimal representation = binary = decimal = signed decimal (...
void p_reset()
In the real RiSC16: R[result] <– PC + 1 (where PC = Program Counter), PC <– 0 (jump to 0) but impos...
void println_infos()
Print to stdout the number of executed instructions.
void println_value32(std::uint32_t n)
Print to stdout the 32 bits value n: hexadecimal representation = binary = decimal = signed decimal...
word16_t memory[256]
Memory items.
std::uint16_t word16_t
Type for register and memory items.
void i_addi(unsigned int result, unsigned int a, immed_t immed6)
(ADD Immediate) R[result] <– R[a] + immed6
void println_registers()
Print all registers items.
void println_reg(unsigned int a)
Print the register R[a].
std::uint16_t immed_t
Type for immediate value.
void print_mem(unsigned int i)
Print ith memory item M[i] (without newline).
void i_add(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] + R[b].
const unsigned int size_memory
Size of the memory: 256 word16_t items.
word16_t registers[8]
Registers.
const unsigned int nb_registers
Number of registers: 8 word16_t items.
unsigned int after_last_memory_acceded
Index following the last memory item used.
void i_sw(unsigned int a, unsigned int result, immed_t immed6)
(Store Word) Memory[R[result] + immed6] <– R[a]
void println_reg2(unsigned int a2, unsigned int a1)
Print the 32 bits value of R[a2]:R[a1].
Instructions set of RiSC16: 8 instructions i_* and 4 pseudo-instructions p_*.
uint64_t nb_executed
Number of instructions executed.
void println_mem(unsigned int i)
Print ith memory item M[i].
void println_value16(std::uint16_t n)
Print to stdout the 16 bits value n: hexadecimal representation = binary = decimal = signed decimal...
void print_reg(unsigned int a)
Print the register R[a] (without newline).
void print_value16(std::uint16_t n)
Print to stdout the 16 bits value n: hexadecimal representation = binary = decimal = signed decimal (...
void clear_registers()
Reset to 0 all registers.
void p_halt(bool print)
If print then call println_all()
void print_reg2(unsigned int a2, unsigned int a1)
Print the 32 bits value of R[a2]:R[a1] (without newline).
void println_all()
Print infos, registers and memory (if used).
void clear_nb_executed()
Reset the number of executed instructions.
void i_nand(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] NAND R[b] (== ~(a & b))
void i_lw(unsigned int result, unsigned int a, immed_t immed6)
(Load Word) R[result] <– Memory[R[a] + immed6]
void i_lui(unsigned int result, immed_t immed10)
(Load Upper Immediate) R[result] <– immed10 << 6