lab5: add assembler and dynamic pipeline system emulator

This commit is contained in:
2026-11-07 20:44:35 +02:00
parent 79a96aea77
commit 36b4596bd4
12 changed files with 401 additions and 7 deletions
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <vector>
#include "expression.hpp"
#include "dyn-pipeline.hpp"
namespace Assembler {
void assemble(
const Expression::TreeNode& root,
std::vector<DynamicPipeline::Command>& program,
std::array<double, DYN_PIPELINE_MEM_SIZE>& memory,
int& mem_used);
}
+62
View File
@@ -0,0 +1,62 @@
#pragma once
#include <array>
#include <vector>
#include <cstdint>
#include "config.h"
namespace DynamicPipeline {
enum CommandType : char
{
ADD,
SUB,
MUL,
DIV,
POW,
CMD_COUNT
};
struct Command
{
enum CommandType t;
uint16_t op1;
uint16_t op2;
uint16_t res;
Command(enum CommandType t, uint16_t op1, uint16_t op2, uint16_t res)
: t(t), op1(op1), op2(op2), res(res)
{
}
};
struct MemCell
{
double v;
int ready;
};
class System
{
private:
std::vector<Command> *program_memory;
std::array<MemCell, DYN_PIPELINE_MEM_SIZE> data_memory;
std::array<int, CMD_COUNT> delays {2, 2, 5, 5, 9};
std::array<int, DYN_PIPELINE_LAYERS> layer_cmd {-1};
unsigned int pc {0};
unsigned int system_time;
void print_pipeline(void) const;
bool pipeline_empty(void) const;
bool next_instruction(void) const;
bool instruction_ready(void) const;
void step(void);
public:
void init(const std::vector<Command> * const prg, const std::array<double, DYN_PIPELINE_MEM_SIZE>& mem, const int init_mem_size);
void run(void);
};
}
+3 -2
View File
@@ -24,7 +24,8 @@ public:
SUB,
MUL,
DIV,
POW
POW,
NOP // not an operator
};
union TokenValue {
@@ -358,7 +359,7 @@ public:
// if value, left and right hands should be null
explicit TreeNode(void)
: l(nullptr), r(nullptr), o()
: l(nullptr), r(nullptr), o(Token::NOP)
{
}
+18 -1
View File
@@ -1,9 +1,10 @@
#pragma once
#include <iostream>
#include <iomanip>
#include "config.h"
#if DEBUG == 1
#if PRINT_DEBUG == 1
#if PRINT_DISABLE_PREFIXES == 1
#define pr_debug(...) std::cout << "debug: " << __VA_ARGS__ << "\n"
#define pri_debug(...) std::cout << __VA_ARGS__
@@ -19,6 +20,22 @@
#define prs_debug(...) do {} while (0)
#endif
#if PRINT_INFO == 1
#if PRINT_DISABLE_PREFIXES == 1
#define pr_info(...) std::cout << "info: " << __VA_ARGS__ << "\n"
#define pri_info(...) std::cout << __VA_ARGS__
#define prs_info(...) std::cout << "info: " << __VA_ARGS__
#else
#define pr_info(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] info: " << __VA_ARGS__ << "\n"
#define pri_info(...) std::cout << __VA_ARGS__
#define prs_info(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] info: " << __VA_ARGS__
#endif
#else
#define pr_info(...) do {} while (0)
#define pri_info(...) do {} while (0)
#define prs_info(...) do {} while (0)
#endif
#if PRINT_DISABLE_PREFIXES == 1
#define pr_warn(...) std::cout << "warning: " << __VA_ARGS__ << "\n"
#else
+2 -2
View File
@@ -8,7 +8,7 @@ namespace Syntax
int prepare_token_vector(std::vector<Expression::Token>& tv);
std::vector<Expression::Operation> build_op_vector(const std::vector<Expression::Token>& tv);
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov);
void print_op_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov);
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token>& tv, const std::vector<Expression::Operation>& ov);
void print_op_tree(const std::vector<Expression::Token>& tv, const std::vector<Expression::Operation>& ov);
}