Skip to content

Instantly share code, notes, and snippets.

@whitequark
Last active December 22, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitequark/5aac46196ac505297bef48adce34fca3 to your computer and use it in GitHub Desktop.
Save whitequark/5aac46196ac505297bef48adce34fca3 to your computer and use it in GitHub Desktop.
JT51 + CXXRTL = <3
// Step 1: Obtain latest Yosys from https://github.com/YosysHQ/yosys/.
// Step 2: Download jt51 from https://github.com/jotego/jt51.
// Step 3: Build as follows:
// $ yosys jt51/hdl/*.v -b 'cxxrtl -header' -o jt51_core.cc
// $ CFLAGS="-fbracket-depth=2048 -I$(yosys-config --datdir/include)"
// $ clang++ -O3 $CFLAGS jt51_core.cc jt51_driver.cc -o jt51_sim
// Step 4: Enjoy!
#include <fstream>
#include <backends/cxxrtl/cxxrtl_vcd.h>
#include "jt51_core.h"
int main() {
cxxrtl_design::p_jt51 top;
cxxrtl::debug_items debug;
top.debug_info(debug);
size_t steps = 1;
std::ofstream waves("jt51.vcd");
cxxrtl::vcd_writer vcd;
vcd.timescale(1, "us");
auto traces = {
"rst", "clk", "cen", "cen_p1", "cs_n", "wr_n", "a0", "din", "busy",
"xleft", "xright",
};
for (auto name : traces)
vcd.add(name, debug.at(name));
top.p_rst .set(1u);
top.p_clk .set(0u);
top.p_cen .set(1u);
top.p_cen__p1.set(1u);
top.p_din .set(0u);
top.p_a0 .set(0u);
top.p_cs__n .set(0u);
top.p_wr__n .set(1u);
top.step();
vcd.sample(steps++);
size_t index = 0;
std::vector<std::pair<uint8_t, uint8_t>> prog = {
{0x20,0xC7},
{0x28,0x4A},
{0x40,0x01},
{0x60,0x00},
{0x80,0x1F},
{0xA0,0x00},
{0xE0,0xFF},
{0x08,0x08},
{0x28,0x4A},
};
while (steps < 100000) {
if (steps < 1000) {
top.p_rst.set(1u);
} else {
top.p_rst.set(0u);
if (index < prog.size()) {
switch (steps % 200) {
case 0:
top.p_a0 .set(0u);
top.p_din .set(prog[index].first);
top.p_wr__n.set(0u);
break;
case 10:
top.p_wr__n.set(1u);
break;
case 20:
top.p_a0 .set(1u);
top.p_din .set(prog[index].second);
top.p_wr__n.set(0u);
break;
case 30:
top.p_wr__n.set(1u);
index++;
break;
}
}
}
top.p_cen__p1.set<bool>(!top.p_cen__p1.get<bool>());
top.p_clk.set(0u);
top.step();
vcd.sample(steps++);
top.p_clk.set(1u);
top.step();
vcd.sample(steps++);
waves << vcd.buffer;
vcd.buffer.clear();
}
return 0;
}
@whitequark
Copy link
Author

waaaaaaves

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment