<jupyter_start><jupyter_text>CoreIRThis notebook uses the `"coreir"` mantle backend on the icestick.We begin by building a normal `Magma` circuit using `Mantle` and the `Loam` `IceStick` board.<jupyter_code>import magma as m
# default mantle target is coreir, so no need to do this unless you want to be explicit
# m.set_mantle_target("coreir")
from mantle import Counter
from loam.boards.icestick import IceStick

icestick = IceStick()
icestick.Clock.on()
icestick.D5.on()

N = 22
main = icestick.main()

counter = Counter(N)
m.wire(counter.O[N-1], main.D5)

m.EndCircuit()<jupyter_output>import lattice ice40<jupyter_text>To compile to `coreir`, we simply set the `output` parameter to the `m.compile` command to `"coreir"`.<jupyter_code>m.compile("build/blink_coreir", main, output="coreir")<jupyter_output><empty_output><jupyter_text>We can inspect the generated `.json` file.<jupyter_code>%cat build/blink_coreir.json<jupyter_output>{"top":"global.main",
"namespaces":{
  "global":{
    "modules":{
      "Add22_cout":{
        "type":["Record",[
          ["I0",["Array",22,"BitIn"]],
          ["I1",["Array",22,"BitIn"]],
          ["O",["Array",22,"Bit"]],
          ["COUT","Bit"]
        ]],
        "instances":{
          "bit_const_0_None":{
            "modref":"corebit.const",
            "modargs":{"value":["Bool",false]}
          },
          "coreir_add23_inst0":{
            "genref":"coreir.add",
            "genargs":{"width":["Int",23]}
          }
        },
        "connections":[
          ["coreir_add23_inst0.in0.22","bit_const_0_None.out"],
          ["coreir_add23_inst0.in1.22","bit_const_0_None.out"],
          ["self.I0.0","coreir_add23_inst0.in0.0"],
          ["self.I0.10","coreir_add23_inst0.in0.10"],
          ["self.I0.11","coreir_add23_inst0.in0.11"],
          ["self.I0.12","coreir_add23_inst0.in0.12"],
          ["self.I0.13","coreir_add23_inst0.in0.13"],
 [...]<jupyter_text>We can use the `coreir` command line tool to generate verilog.<jupyter_code>%%bash
coreir -i build/blink_coreir.json -o build/blink_coreir.v<jupyter_output>/Users/hanrahan/git/coreir/src/binary/coreir.cpp:188 Running Runningvpasses
/Users/hanrahan/git/coreir/src/passes/transform/rungenerators.cpp:10 In Run Generators
/Users/hanrahan/git/coreir/src/passes/transform/rungenerators.cpp:26 Done running generators
/Users/hanrahan/git/coreir/src/binary/coreir.cpp:197 Running vpasses
/Users/hanrahan/git/coreir/src/binary/coreir.cpp:238 Modified?: No<jupyter_text>And now we can inspect the generated verilog from coreir, notice that includes the `verilog` implementations of all the coreir primitives.<jupyter_code>%cat build/blink_coreir.v
%%bash
cd build
yosys -q -p 'synth_ice40 -top main -blif blink_coreir.blif' blink_coreir.v
arachne-pnr -q -d 1k -o blink_coreir.txt -p blink_coreir.pcf blink_coreir.blif 
icepack blink_coreir.txt blink_coreir.bin
#iceprog blink_coreir.bin<jupyter_output>/Users/hanrahan/git/magmathon/notebooks/advanced/build
{'fraction_non_alphanumeric': 0.14913448735019974, 'fraction_numerical': 0.03195739014647137, 'mean_word_length': 3.8389694041867957, 'pattern_counts': {'":': 16, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '203459', 'license': 'MIT', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1093, 'n_words': 195, 'path': 'notebooks/advanced/coreir.ipynb', 'repo_name': 'phanrahan/magmathon', 'size': '3004'}