upsilon/firmware/rtl/control_loop/control_loop_sim.cpp

86 lines
1.8 KiB
C++
Raw Normal View History

2022-09-16 18:01:34 -04:00
#include <memory>
#include <limits>
#include <cstdint>
2022-10-17 00:44:30 -04:00
#include <cstring>
#include <cstdlib>
2022-09-16 18:01:34 -04:00
#include <iostream>
#include <random>
2022-10-17 00:44:30 -04:00
#include <unistd.h>
2022-09-16 18:01:34 -04:00
#include <verilated.h>
2022-11-21 21:41:50 -05:00
#include "control_loop_math_implementation.h"
#include "control_loop_cmds.h"
#include "Vcontrol_loop_sim_top.h"
using ModType = Vcontrol_loop_sim_top;
uint32_t main_time = 0;
double sc_time_stamp() {
return main_time;
2022-10-17 00:44:30 -04:00
}
2022-11-21 21:41:50 -05:00
ModType *mod;
2022-10-17 00:44:30 -04:00
2022-11-21 21:41:50 -05:00
static void run_clock() {
for (int i = 0; i < 2; i++) {
mod->clk = !mod->clk;
mod->eval();
main_time++;
2022-10-17 00:44:30 -04:00
}
}
2022-11-21 21:41:50 -05:00
static void init(int argc, char **argv) {
Verilated::commandArgs(argc, argv);
Verilated::traceEverOn(true);
mod = new ModType;
mod->clk = 0;
}
2022-10-17 00:44:30 -04:00
2022-11-21 21:41:50 -05:00
static void set_value(V val, unsigned name) {
mod->cmd = CONTROL_LOOP_WRITE_BIT | name;
mod->word_into_loop = val;
mod->start_cmd = 1;
2022-10-17 00:44:30 -04:00
2022-11-21 21:41:50 -05:00
do { run_clock(); } while (!mod->finish_cmd);
mod->start_cmd = 0;
run_clock();
2022-10-17 00:44:30 -04:00
}
2022-11-21 21:41:50 -05:00
int main(int argc, char **argv) {
init(argc, argv);
mod = new ModType;
Transfer func = Transfer{150, 0, 2, 1.1, 10, -1};
2022-10-17 00:44:30 -04:00
2022-11-21 21:41:50 -05:00
mod->clk = 0;
2022-10-17 00:44:30 -04:00
2022-11-21 21:41:50 -05:00
set_value(10000, CONTROL_LOOP_STATUS);
set_value(0b11010111000010100011110101110000101000111, CONTROL_LOOP_P);
set_value((V)12 << CONSTS_FRAC, CONTROL_LOOP_I);
set_value(20, CONTROL_LOOP_DELAY);
2022-11-21 22:04:46 -05:00
set_value(10000, CONTROL_LOOP_SETPT);
2022-11-21 21:41:50 -05:00
set_value(1, CONTROL_LOOP_STATUS);
2022-11-21 22:24:37 -05:00
for (int tick = 0; tick < 100000; tick++) {
2022-11-21 21:41:50 -05:00
std::cout << tick << std::endl;
run_clock();
if (mod->request && !mod->fulfilled) {
mod->measured_value = func.val(mod->curset);
mod->fulfilled = 1;
} else if (mod->fulfilled && !mod->request) {
mod->fulfilled = 0;
}
2022-10-17 00:44:30 -04:00
2022-11-21 22:24:37 -05:00
if (tick == 50000) {
2022-11-21 21:41:50 -05:00
mod->cmd = CONTROL_LOOP_WRITE_BIT | CONTROL_LOOP_P;
mod->word_into_loop = 0b010111000010100011110101110000101000111;
mod->start_cmd = 1;
}
if (mod->finish_cmd) {
mod->start_cmd = 0;
2022-10-17 00:44:30 -04:00
}
}
2022-11-21 21:41:50 -05:00
mod->final();
delete mod;
return 0;
2022-09-16 18:01:34 -04:00
}