<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Nairen&apos;s Blog</title>
    <description></description>
    <link>https://naiiren.github.io/</link>
    <atom:link href="https://naiiren.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 18 Aug 2025 12:11:19 +0000</pubDate>
    <lastBuildDate>Mon, 18 Aug 2025 12:11:19 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>所见即所得的数字电路教程</title>
        <description>&lt;p&gt;数字电路系统是现代计算机与电子工业工程的基础。本教程将介绍如何使用一种新兴的硬件描述语言 NetX 进行数字电路设计。在设计和实现数字电路时，我们脑海里总是会先有一个大概的电路图，接着使用硬件描述语言来描绘它——而 NetX 代码的组织形式与电路图的结构非常相似，用 NetX 编码几乎就是所见即所得地描述电路图的过程。&lt;/p&gt;

&lt;p&gt;本教程只作简单介绍。更多细节可以参考 NetX 的语言手册。&lt;/p&gt;

&lt;h2 id=&quot;语言概览&quot;&gt;语言概览&lt;/h2&gt;
&lt;h3 id=&quot;组合电路&quot;&gt;组合电路&lt;/h3&gt;
&lt;p&gt;我们首先介绍 NetX 中最基础的两个运算符，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;||&lt;/code&gt; 与 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;&amp;lt;&amp;gt;&lt;/code&gt;，分别称作并行组合子（Parallel Combinator）与串行组合子（Sequential Combinator）。简单来说，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;||&lt;/code&gt; 就像把两个电路元件并排放置，而 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;&amp;lt;&amp;gt;&lt;/code&gt; 就像用导线连接元件的输出和输入。&lt;/p&gt;

&lt;p&gt;下面我们小试牛刀，用这两个运算符把两个 2-1 与门组合成一个 4-1 与门：&lt;/p&gt;

&lt;center&gt;
&lt;img src=&quot;https://naiiren.github.io/assets/and.svg&quot; alt=&quot;and&quot; style=&quot;zoom:200%;&quot; /&gt;
&lt;/center&gt;

&lt;p&gt;我们从一个 2-1 与门 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;AND&lt;/code&gt; 出发，首先用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;||&lt;/code&gt; 并列连接两个与门，这就得到了一个 4-2 的特殊电路元件。这个元件的内部有两个并列的与门，分别处理整个元件的前两个、后两个输入并分别输出。接下来，我们用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;&amp;lt;&amp;gt;&lt;/code&gt; 将这个元件的输出串接到第三个 2-1 与门上，也就得到了一个 4-1 的电路元件——也就是一个 4-1 与门。&lt;/p&gt;

&lt;p&gt;使用 NetX 内置的、包括 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;AND&lt;/code&gt; 的各种基础逻辑门，我们还可以组合出稍复杂一些的元件。&lt;/p&gt;

&lt;p&gt;比如，可以通过下面的代码实现一个半加器。半加器是一种基本的数字电路元件，它实现了两个 1-bit 二进制数的加法。不过要注意，1-bit 二进制数只能是 0 或 1，而他们的和 2 需要 2-bit 才能表示（表示为 10）。这里，我们把两个输入 1-bit 二进制数记作 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b&lt;/code&gt;，对于他们的 2-bit 和，我们将高位记作 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;carry&lt;/code&gt;（进位），低为记作 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sum&lt;/code&gt;（和）。具体来说，半加器的逻辑关系如下表所示：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;a&lt;/th&gt;
      &lt;th&gt;b&lt;/th&gt;
      &lt;th&gt;carry&lt;/th&gt;
      &lt;th&gt;sum&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;仔细观察，我们发现 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;carry&lt;/code&gt; 实际上可以由一个与门得到，而 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sum&lt;/code&gt; 可以由一个异或门得到：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;component HA : [a, b] -&amp;gt; [sum, carry] {
	wire a, b, sum, carry of bit(1);
	  sum &amp;lt;&amp;gt; XOR &amp;lt;&amp;gt; (a || b); 
	carry &amp;lt;&amp;gt; AND &amp;lt;&amp;gt; (a || b);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这里，语句 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;wire a, b, sum, carry of bit(1);&lt;/code&gt; 定义了四个 1-bit 的“电线”元件。电线可以理解为一种特殊的 1-1 元件，它会直接将得到的输入传递到输出端口。如果一个电线的名字（比如 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a&lt;/code&gt;）在代码中出现了多次，我们会认为他们是同一根电线。比如在上面的代码中，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b&lt;/code&gt; 分别出现了两次，代表着与门和异或门的输入是相同的电线。&lt;/p&gt;

&lt;p&gt;上述代码刻画的电路图如下所示：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://naiiren.github.io/assets/HA.svg&quot; alt=&quot;HA&quot; style=&quot;zoom:200%;&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;img src=&quot;https://naiiren.github.io/assets/add.svg&quot; alt=&quot;add&quot; style=&quot;zoom:200%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;半加器实现了 1-bit 二进制数的加法，接着我们来实现 4-bit 甚至更复杂的二进制加法。假设我们现在有 4-bit 宽的输入 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b&lt;/code&gt;，要输出 4-bit 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;c&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;最简单的思路是从低位向高位依次计算每一个二进制位的和，并将可能产生的进位向后传递。这里“每一个二进制位的和”可以用刚刚实现的半加器来实现。&lt;/p&gt;

&lt;p&gt;我们用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]&lt;/code&gt; 表示输入 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a&lt;/code&gt; 的第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 位。注意：在计算每一位二进制的和时，除了要考虑当前的输入 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b[i]&lt;/code&gt;，我们还需要考虑前一位加法进位 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;cin&lt;/code&gt;。而半加器只能处理两个输入，我们还需要实现一个能将三个 1-bit 二进制数加起来的元件——也就是全加器。&lt;/p&gt;

&lt;p&gt;在实现全加器之前，我们先来了解一个特殊的内置元件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;TO&lt;/code&gt;，它可以理解为匿名的”电线”。一般有名字的电线多次出现时，NetX 会认为他们总是同一根电线；但是 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;TO&lt;/code&gt; 多次出现时，它们总是不同的电线。&lt;/p&gt;

&lt;p&gt;利用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;TO&lt;/code&gt;，我们可以用下面的方式，十分简洁地实现一个全加器：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;let FA = (TO || OR) &amp;lt;&amp;gt; (HA || TO) &amp;lt;&amp;gt; (TO || HA);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这行代码乍看之下让人有些摸不着头脑——但结合图片左上角的全加器电路图来看，就清楚多了。&lt;/p&gt;

&lt;p&gt;全加器是将两个半加器前后组合起来实现 3-bit 加法的元件，而上面的代码就描述了这种“前后组合”的图形模式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;面对三个输入信号，我们将其中一个留空，将另外两个传入半加器 FA&lt;/li&gt;
  &lt;li&gt;让刚刚留空的信号也参与到加法中：将它与 FA 的 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sum&lt;/code&gt; 位通过另一个 FA 相加&lt;/li&gt;
  &lt;li&gt;此时，两个 FA 的进位位 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;carry&lt;/code&gt; 还没有处理。由于三个 1-bit 加法的性质，这两个 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;carry&lt;/code&gt; 只可能有一个为 1，因此我们可以用一个或门 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;OR&lt;/code&gt; 将它们的输出相加，得到最终的进位输出。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;当然，这样的写法有些抽象，刚刚接触 NetX 语言难免会有些不适应。&lt;/p&gt;

&lt;p&gt;我们也可以用更传统的方式，给每个元件的输入输出端口起个名字，写成下面的样子：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;component FA : [a, b, cin] -&amp;gt; [sum, cout] {
    wire a, b, cin, sum, cout of bit(1);
    wire tmp_sum, c1, c2 of bit(1);
    (tmp_sum || c1) &amp;lt;&amp;gt; HA &amp;lt;&amp;gt; (b || cin);
        (sum || c2) &amp;lt;&amp;gt; HA &amp;lt;&amp;gt; (a || tmp_sum);
    cout &amp;lt;&amp;gt; OR &amp;lt;&amp;gt; (c1 || c2);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;实现了全加器，只需要把他们的进位串联起来，就能实现 4-bit 甚至更复杂的二进制加法了。&lt;/p&gt;

&lt;p&gt;我们考虑第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 个全加器，它需要将 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b[i]&lt;/code&gt; 相加，把结果传递给 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;c[i]&lt;/code&gt;，把进位传递给第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i+1&lt;/code&gt; 个全加器。我们用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;pattern(i)&lt;/code&gt; 表示这种连线的模式：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;let pattern(i) =
	(#c[i] || TO) &amp;lt;&amp;gt; FA &amp;lt;&amp;gt; (a[i]# || b[i]# || TO);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这里，我们将 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]&lt;/code&gt;, &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b[i]&lt;/code&gt;, &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;c[i]&lt;/code&gt; 绑定到全加器的端口上，并且把进位输入和输出留空，之后再处理。&lt;/p&gt;

&lt;p&gt;这里出现了一个新的运算符 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;#&lt;/code&gt;，它叫作端口装饰器（Port Decorator），可以用来“堵住”某个元件的输入输入端口。比如&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;#c[i]&lt;/code&gt; 表示只使用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;c[i]&lt;/code&gt; 的输入端口，堵住它的输出端口&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]#&lt;/code&gt; 表示只使用 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]&lt;/code&gt; 的输出端口，堵住它的输入端口&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在全加器的这种连线模式中， &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;a[i]&lt;/code&gt; 和  &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;b[i]&lt;/code&gt; 是整个模块的输入，它们的输入端口应该留空以待后续使用。我们不希望他们在加法器模块里就被占用，干脆用一个 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;#&lt;/code&gt; 把端口堵上，让其他元件没法与他们相连。对于 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;c[i]&lt;/code&gt; 也是类似，只不过我们堵上的是它的输出端口。&lt;/p&gt;

&lt;p&gt;好了，这样我们定义了 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;pattern(i)&lt;/code&gt;，它是一种新的 1-1 的元件，接受进位输入 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;cin&lt;/code&gt;，输出新的进位 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;cout&lt;/code&gt;。把初始进位设置为 1-bit 的 0，然后把 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;pattern(i)&lt;/code&gt; 首尾相连，也就得到了我们想要的 4-bit 加法器：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;component ADDER : [a, b] -&amp;gt; [c] {
	wire a, b, c of bit(4);
	let FA = /* ... */;
	let pattern(i) = /* ... */;
	let adder(i) =
		if i &amp;lt; 0 then 1&apos;b0
		else pattern(i) &amp;lt;&amp;gt; adder(i - 1);
	adder(3);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这里我们用到了递归的写法。&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;adder(3)&lt;/code&gt; 会被一步展开成&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;pattern(3) &amp;lt;&amp;gt; adder(2)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;接着一步步变成&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;pattern(3) &amp;lt;&amp;gt; pattern(2) &amp;lt;&amp;gt; pattern(1) &amp;lt;&amp;gt; pattern(0) &amp;lt;&amp;gt; 1&apos;b0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;从而实现了这个 4-bit 的加法器。&lt;/p&gt;

&lt;p&gt;当然，如果你不熟悉递归的写法，也可以引入一个中间变量，写成下面的样子：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;component ADDER : [a, b] -&amp;gt; [c] {
	wire a, b, c of bit(4);
	wire carry of bit(4);
	let cin(i) = if i == 0 then 1&apos;b0 else carry[i - 1];
	for (i in [0..4]) {
		(c[i] || carry[i]) &amp;lt;&amp;gt; FA &amp;lt;&amp;gt; (
			a[i] || b[i] || cin(i)
		);	
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;hr /&gt;

&lt;p&gt;下面我们考虑另外一种常见的组合电路：编码器（Encoder），我们常常用它将热独码转为二进制码。&lt;/p&gt;

&lt;p&gt;热独码（One-hot code）指的是考虑有 \(2^n\) 个 bit 的输入，其中只有一个 bit 为 1，其余的都为 0。假设其中第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 个 bit 为 1，那么编码器就是要输出 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 的 n-bit 二进制表示。&lt;/p&gt;

&lt;p&gt;下面展示了一个 4-2 编码器的电路图：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://naiiren.github.io/assets/encoder.svg&quot; alt=&quot;encoder&quot; style=&quot;zoom:200%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;编码器的电路图乍看之下有些复杂，但仔细观察也能发现其中的规律：既然只有一个输入为 1，那么让这一位信号传递到应该为 1 的输出端口就可以了。而&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;假设输入的第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 位为 1&lt;/li&gt;
  &lt;li&gt;第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;j&lt;/code&gt; 个输出端口输出的是 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 的二进制表示的第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;j&lt;/code&gt; 位&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;也就是说，如果 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 的第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;j&lt;/code&gt; 位为 1（换言之，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;(i &amp;gt;&amp;gt; j) &amp;amp; 1 == 1&lt;/code&gt;），那么第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;j&lt;/code&gt; 个输出就会和第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 个输入相连。我们可以用下面的代码来表达这种模式：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;component ENCODER(n) : [input] -&amp;gt; [output] {
	let power = 2 ** n;
	wire input of bit(power);
	wire output of bit(n);
	for (j in [0..n]) {
		output[j] &amp;lt;&amp;gt; OR &amp;lt;&amp;gt; [
			input[i] | i in [0..power], 
			           when (i &amp;gt;&amp;gt; j) &amp;amp; 1 == 1
		];
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;我们定义了一个编码器模块 ENCODER，它含有一个属性 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;n&lt;/code&gt; 指示：输入的编码有 \(2^n\) 位，输出的编码有 \(n\) 位；我们依照这个规约定义 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;output&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;接着来考虑每个输出位 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;output[j]&lt;/code&gt; 的来源。这里出现了一种新的表达式：列表推导（List Comprehension）。&lt;/p&gt;

&lt;p&gt;列表在 NetX 可以作为一种语法糖。列表 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[e1, e2, ..., en]&lt;/code&gt; 也就表示 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;(e1 || e2 || ... || en)&lt;/code&gt; 这样许多个电路元件并列组合在一起的意思。只是这样看，列表和原来的写法好像也没有什么差别。但是列表还支持下面的写法：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;[expr | qualifier_1, ..., qualifier_n]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这代表着根据后面列出的 qualifier 生成一个新的列表。比如，&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;[i**2 | i in [0..6], when i % 2 == 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;就生成了 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[1, 9, 25]&lt;/code&gt; 这样一个列表。这里的 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i in [0..6]&lt;/code&gt; 表示 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 在 \(0\) 到 \(6\) 之间取值，而 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;when i % 2 == 1&lt;/code&gt; 则表示只有当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i&lt;/code&gt; 是奇数时才会把 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;i**2&lt;/code&gt; 加入到列表中。&lt;/p&gt;

&lt;p&gt;具体地，在上面的编码器代码中，列表推导&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;[input[i] | i in [0..power], when (i &amp;gt;&amp;gt; j) &amp;amp; 1 == 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;在 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;n = 2&lt;/code&gt; 即 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;power = 4&lt;/code&gt; 时，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如固定 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;j = 0&lt;/code&gt;，则得到 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[input[1], input[3]]&lt;/code&gt;，也就是 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;(input[1] || input[3])&lt;/code&gt;，这是第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;0&lt;/code&gt; 位输出的来源；&lt;/li&gt;
  &lt;li&gt;如固定 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;j = 1&lt;/code&gt;，则得到 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[input[2], input[3]]&lt;/code&gt;，也就是 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;(input[2] || input[3])&lt;/code&gt;，这是第 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;1&lt;/code&gt; 位输出的来源。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;通过类似的列表推导，我们就可以描述电路中复杂的连接模式，从而像上面那样简洁地实现一个编码器。&lt;/p&gt;

&lt;h3 id=&quot;时序电路&quot;&gt;时序电路&lt;/h3&gt;

&lt;p&gt;到目前为止，我们已经大概了解了怎么使用 NetX 设计组合电路。接下来我们看看如何设计时序电路，也就是含有寄存器等存储元件，可以记录状态的电路。&lt;/p&gt;

&lt;p&gt;NetX 内置了一个简单的寄存器 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;REGISTER&lt;/code&gt;，使用时大概像是这样：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;output &amp;lt;&amp;gt; REGISTER(pos_edge = true) &amp;lt;&amp;gt; [clk, input];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;寄存器有两个输入信号，记作 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;clk&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input&lt;/code&gt;。其中 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;clk&lt;/code&gt; 是时钟信号：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果寄存器是上升沿触发的（&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;pos_edge = true&lt;/code&gt;），那么当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;clk&lt;/code&gt; 从 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;0&lt;/code&gt; 变为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;1&lt;/code&gt; 时，寄存器才会把当前 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input&lt;/code&gt; 的值存储下来。&lt;/li&gt;
  &lt;li&gt;如果寄存器是下降沿触发的（&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;pos_edge = false&lt;/code&gt;），那么当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;clk&lt;/code&gt; 从 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;1&lt;/code&gt; 变为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;0&lt;/code&gt; 时，寄存器才会把当前 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input&lt;/code&gt; 的值存储下来。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;寄存器的输出信号 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;output&lt;/code&gt; 就是当前寄存器内部存储的值。&lt;/p&gt;

&lt;p&gt;在实际应用中，我们经常需要包含复位信号的寄存器。&lt;/p&gt;

&lt;p&gt;复位信号可以在系统启动或出错时将寄存器强制设置为已知的初始值：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;import std.selector.MUX; // 从标准库里引入多路选择器 MUX

/**
  * Register component
  *
  * This component implements a register with synchronous reset.
  *
  * @attr pos_edge   Triggers on rising edge of clock when true
  * @attr high_rst   Enables active-high reset when true
  * @attr rst_value  Value to load on reset
  *
  * @port clk        Clock signal
  * @port rst        Reset signal
  * @port input      Input data to be registered
  * @port output     Output data from the register
  */
component REG(pos_edge, high_rst, rst_value) : [clk, rst, input] -&amp;gt; [output] {
	wire clk of clock(); // 声明为时钟信号，也可以直接写成 wire clk of clock;
	auto input, output;  // 让编译器根据使用场景自动推断位宽
	
	output &amp;lt;&amp;gt; REGISTER(pos_edge) &amp;lt;&amp;gt; [
		clk, MUX(1) &amp;lt;&amp;gt; (
			if high_rst 
			then [rst, input, rst_value]
			else [rst, rst_value, input]
		)
	];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这里 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX&lt;/code&gt; 是数字电路中的条件选择模块，我们已经在 NetX 的标准库里实现了一份。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX(1)&lt;/code&gt; 元件有三个输入，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sel&lt;/code&gt;, &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input_0&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input_1&lt;/code&gt;。这里传入的属性 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;1&lt;/code&gt; 代表了选择信号 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sel&lt;/code&gt; 的宽度：1-bit 的选择信号可以在两个分支输入中选择一个输出：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;若 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sel = 0&lt;/code&gt;，则选择器输出 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input[0]&lt;/code&gt; 的值&lt;/li&gt;
  &lt;li&gt;若 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;sel = 1&lt;/code&gt;，则选择器输出 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input[1]&lt;/code&gt; 的值&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;特别地，在我们的复位寄存器中，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX(1)&lt;/code&gt; 的参数顺序会依据 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;high_rst&lt;/code&gt; 的值修改：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;high_rst = true&lt;/code&gt; 时，意味着当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;rst&lt;/code&gt; 信号为 1 时重置寄存器的值，于是 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX&lt;/code&gt; 的参数顺序应当为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[rst, input, rst_value]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;high_rst = false&lt;/code&gt; 时，意味着当 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;rst&lt;/code&gt; 信号为 0 时重置寄存器的值，于是 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX&lt;/code&gt; 的参数顺序应当为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[rst, rst_value, input]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在使用这个含有复位信号的寄存器时，我们就可以写成：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;my_output &amp;lt;&amp;gt; REG(true, true, 0) &amp;lt;&amp;gt; [my_clk, my_rst, my_input]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;或者为了让代码更清晰，也可以显式地指定属性、输入、输出的名字，写成：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;{output : my_output} &amp;lt;&amp;gt; REG (
	pos_edge = true, 
	high_rst = true, 
	rst_value = 0
) &amp;lt;&amp;gt; {
	clk : my_clk,
	rst : my_rst,
	input : my_input
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;这种写法可以看清每个信号的用途，在复杂的电路中会特别有用。&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;现在我们就可以利用寄存器设计时序电路了。&lt;/p&gt;

&lt;p&gt;我们先来看一个经典的例子：用时序电路实现一个简单的红绿灯状态机。&lt;/p&gt;

&lt;p&gt;它有红绿黄三种状态，红灯亮 30 秒时将会变为绿灯，绿灯亮 25 秒后变为黄灯，黄灯亮 5 秒后变为红灯。我们可以用一个状态转移图来描述这个过程：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://naiiren.github.io/assets/traffic.svg&quot; alt=&quot;traffic&quot; style=&quot;zoom:200%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;假设我们已经有了一个周期为一秒的时钟信号；对照状态转移图，我们可以设计出下面的电路：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;import std.utils.REG;

// 定义枚举类型，包含红绿黄三种状态
// 编译器会自动推断 State 的位宽为 2
// 并且分配 RED=2&apos;b00, GREEN=2&apos;b01, YELLOW=2&apos;b10
enum State {
  RED, GREEN, YELLOW
};

// 计数器组件，用来记录距离上次变灯经过的时间
component COUNTER : [clk, rst] -&amp;gt; [count] {
	wire count of bit(32);
	wire clk of clock();
	wire rst of bit(1);
	
	// 当 rst 信号到来时重置为 0，否则每个时钟周期加 1
	count &amp;lt;&amp;gt; REG (
		pos_edge=true, high_rst=true, rst_value=0
		) &amp;lt;&amp;gt; [clk, rst, ADD &amp;lt;&amp;gt; [count, 1]];
}

// 红绿灯状态机组件
component TRAFFIC_LIGHT : [clk] -&amp;gt; [color] {
	wire color of State;   // 定义当前颜色 color，位宽为 2
	wire clk of clock();
	
	wire change of bit(1);	// 判断是否需要变灯
	auto counter &amp;lt;&amp;gt; COUNTER &amp;lt;&amp;gt; [clk, change];	// 变灯时需要重置计数器
	change &amp;lt;&amp;gt; EQ &amp;lt;&amp;gt; [	// 如果计数器的值等于当前颜色对应的阈值，则需要变灯
		counter,
			MUX(2) &amp;lt;&amp;gt; {	// 根据当前颜色，决定变灯时的计数器阈值
			sel          : color, 
			case[RED]    : 30, 
			case[GREEN]  : 25, 
			case[YELLOW] : 5, 
			$default     : 0
		}
	];
	
	let clocked_reg = REGISTER(pos_edge = true) &amp;lt;&amp;gt; [TO, clk#]; 
	color &amp;lt;&amp;gt; clocked_reg &amp;lt;&amp;gt; MUX(1) &amp;lt;&amp;gt; [change,	// 用于保存当前颜色状态的寄存器
		color,	// 如果无需变灯，保持当前颜色
		MUX(2) &amp;lt;&amp;gt; {	// 否则，根据当前颜色决定下一个颜色
			sel          : color, 
			case[RED]    : GREEN, 
			case[GREEN]  : YELLOW, 
			case[YELLOW] : GREEN, 
			$default     : RED
		}
	];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;注意，这里 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX&lt;/code&gt; 又用到了按名字连接的语法。这里的表达式&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;MUX(2) &amp;lt;&amp;gt; {	// 根据当前颜色，决定变灯时的计数器阈值
	sel          : color, 
	case[RED]    : 30, 
	case[GREEN]  : 25, 
	case[YELLOW] : 5, 
	$default     : 0
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;实际上等价于 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX(2) &amp;lt;&amp;gt; [color, 30, 25, 5, 0]&lt;/code&gt;，几个输入分别对应 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;MUX(2)&lt;/code&gt; 模块的输入端口 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;[sel, case[0], case[1], case[2], case[3]]&lt;/code&gt;。使用按名连接的方式让代码变得更清晰了。&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;NetX 的设计的特点在于“所见即所得”：&lt;/p&gt;

&lt;p&gt;就算你不理解电路背后的原理，只要从电路图出发一笔一画地临摹，就能将电路实现出来。&lt;/p&gt;

&lt;p&gt;这里我们借用 &lt;a href=&quot;https://doi.org/10.1109/ICCD.1996.563604&quot;&gt;ICCD’96&lt;/a&gt; 的一篇论文提出的开平方电路作为例子。文章中给出了这样一个设计，它每个周期处理两位输入信号，用总计 16 个时钟周期计算 32 位数的平方根。&lt;/p&gt;

&lt;p&gt;电路图如下所示：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://naiiren.github.io/assets/sqrt.png&quot; alt=&quot;sqrt&quot; style=&quot;zoom:50%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;即便你没有读过这篇论文、也完全不理解这个开方算法的原理，还是可以用 NetX 绘制这个电路图；之后就可以通过 NetX 的编译器、仿真器等设施真正地实现这个电路。&lt;/p&gt;

&lt;p&gt;下面是这个开平方电路的 NetX 代码实现，注意观察代码中的各个元件和电路图的对应关系：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-netx&quot;&gt;import std.memory.SHIFT_REG; // 从标准库引入移位寄存器 SHIFT_REG
import std.utils.REG;

component SQRT32(pos_edge, high_rst) : [clk, rst, D] -&amp;gt; [Q] {
  wire clk of clock();
  wire rst of bit(1);
  wire D of bit(32);
  wire Q of bit(16);

  wire R, R_next, lhs, rhs of bit(18);
  let shift(init_value) = SHIFT_REG(16, pos_edge, high_rst, init_value) &amp;lt;&amp;gt; [clk#, rst#, TO];
  
  // 这里 CONCAT 是一种内置组件，它将所有输入信号前后连接成一个更宽的信号
  auto D_odd &amp;lt;&amp;gt; shift(CONCAT &amp;lt;&amp;gt; [D[i * 2 + 1] | i in [16..0]]) &amp;lt;&amp;gt; 1&apos;b0;
  auto D_eve &amp;lt;&amp;gt; shift(CONCAT &amp;lt;&amp;gt; [D[i * 2]     | i in [16..0]]) &amp;lt;&amp;gt; 1&apos;b0;
  Q &amp;lt;&amp;gt; shift(16&apos;d0) &amp;lt;&amp;gt; NOT &amp;lt;&amp;gt; R_next[17];

  lhs &amp;lt;&amp;gt; CONCAT &amp;lt;&amp;gt; [R[15:0], D_odd[15], D_eve[15]];
  rhs &amp;lt;&amp;gt; CONCAT &amp;lt;&amp;gt; [Q, R[17], 1&apos;b1];
  R_next &amp;lt;&amp;gt; MUX(1) &amp;lt;&amp;gt; [
    R[17],
    SUB &amp;lt;&amp;gt; [lhs, rhs],
    ADD &amp;lt;&amp;gt; [lhs, rhs]
  ];
  R &amp;lt;&amp;gt; REG(pos_edge, high_rst, 18&apos;d0) &amp;lt;&amp;gt; [
    clk, rst, R_next
  ];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;可以发现，电路图中的每一个组件（框图）、每一条线网（连线），在 NetX 代码中均有明确的对应。比如电路图中将输入信号 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;D&lt;/code&gt; 分成两部分，分别传入了一个移位寄存器；在代码中也就有 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;D_odd&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;D_eve&lt;/code&gt; 与之对应。&lt;/p&gt;

&lt;h2 id=&quot;工具链使用&quot;&gt;工具链使用&lt;/h2&gt;

&lt;p&gt;上文中，我们大致了解了如何用 NetX 语言设计电路。下面我们将介绍 NetX 目前的工具链，如何用这些工具编译、仿真、测试 NetX 程序，并将它们部署到 FPGA 上。&lt;/p&gt;

&lt;p&gt;这里我们以项目 &lt;a href=&quot;https://github.com/naiiren/NetX-RISC-V&quot;&gt;NetX-RISC-V&lt;/a&gt; 为例，介绍这一过程。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;该项目实现了一个单周期的 RISC-V 处理器。该处理器实现了除 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;FENCE&lt;/code&gt; 以外的所有 RV32I 指令。&lt;/li&gt;
  &lt;li&gt;它可以被部署在一块 Terasic DE2-115 FPGA 开发版上，并使用其板载存储作为指令、数据存储器。&lt;/li&gt;
  &lt;li&gt;它可以使用 C++ 编写的 testbench 进行仿真测试。该 testbench 使用：
    &lt;ul&gt;
      &lt;li&gt;RISC-V 官方测试集中的指令单元测试程序作为激励&lt;/li&gt;
      &lt;li&gt;C++ 实现的软件模拟板载存储器&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;具体电路图与实现可以点击上面的链接查看，这里我们主要介绍工具链的使用。&lt;/p&gt;

&lt;h3 id=&quot;编译与包管理&quot;&gt;编译与包管理&lt;/h3&gt;

&lt;p&gt;NetX-RISC-V 项目中包含了一个配置文件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;_netx.toml&lt;/code&gt; 如下：&lt;/p&gt;

&lt;div class=&quot;language-toml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;[project]&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rv32i&quot;&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0.1.0&quot;&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rv32i.nx&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nn&quot;&gt;[dependencies]&lt;/span&gt;
&lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0.1.0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;git&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;git@github.com:pascal-lab/NetX-std.git&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;它声明了该项目的名称、版本号、包含的源文件。特别地，该项目只包含一个单文件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;rv32i.nx&lt;/code&gt;。此外，该文件包含了一个依赖项 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;std&lt;/code&gt;，并指定了 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;git&lt;/code&gt; 仓库地址。&lt;/p&gt;

&lt;p&gt;包管理器会自动解析配置文件，分析依赖项（如果是非本地的依赖，会自动下载）并进行编译。&lt;/p&gt;

&lt;p&gt;这里，可以使用下面的指令进行编译。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nx compile _netx.toml &lt;span class=&quot;nt&quot;&gt;--top&lt;/span&gt; CORE &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; out.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;编译会产生一个 JSON 文件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;out.json&lt;/code&gt;，它是硬件图的底层表示。&lt;/p&gt;

&lt;p&gt;关于更多的编译选项与配置，参见 &lt;a href=&quot;https://github.com/pascal-lab/NetX&quot;&gt;NetX 仓库&lt;/a&gt;。&lt;/p&gt;

&lt;h3 id=&quot;nxon-底层表示&quot;&gt;Nxon 底层表示&lt;/h3&gt;

&lt;p&gt;Nxon 是基于 JSON 的硬件图底层表示，它是 NetX 编译器的标准输出格式。Nxon 文件主要包含个元件的实例化以及端口连接情况。下面展示了处理器的 Nxon 表示，其中许多地方有省略：&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;totalData&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1063&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;top_input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;clk&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rst&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;instr&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dmem_out&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;top_output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;imem_addr&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dmem_addr&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dmem_in&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dmem_op&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dmem_wr&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;design&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;CORE&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rv32i.nx:7:10&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;clk@0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rst@1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;instr@2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dmem_out@3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;imem_addr@4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;result@5&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Rb@6&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ctl.mem_op@7&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ctl.mem_wr@8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;members&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;clock&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;clk@0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rv32i.nx:8:9&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;wire&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;width&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rst@1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rv32i.nx:9:9&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;wire&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;width&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;instr@2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;rv32i.nx:10:9&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;INSTR_DECODER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;IMM_SELECTOR&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;REG_FILE&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;19:10&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;attribute&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;high_rst&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;members&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;DECODER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;MUX&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;builtin&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;AND&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这一表示说明了顶层元件为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;CORE&lt;/code&gt;，它内部包含了指令译码器 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;INSTR_DECODER&lt;/code&gt;，立即数选择器 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;IMM_SELECTOR&lt;/code&gt;，寄存器堆 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;REG_FILE&lt;/code&gt; 等元件。其中每个元件又包含了各自的子元件。&lt;/p&gt;

&lt;p&gt;元件含有下面的属性：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;type&lt;/code&gt;：JSON 对象的类型。对于用户定义的元件，该项设置为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;component&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;id&lt;/code&gt;：该元件在定义时的唯一标识符&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;loc&lt;/code&gt;：该元件定义的源码位置，格式为 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;文件名:行号:列号&lt;/code&gt;。如 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;&quot;loc&quot; : &quot;rv32i.nx:7:10&quot;&lt;/code&gt; 表示该元件的定义在源代码文件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;rv32i.nx&lt;/code&gt; 的第 7 行第 10 列。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;input&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;output&lt;/code&gt;：指示该元件的输入输出线。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;members&lt;/code&gt;：该元件的内部结构。可能包括线网的定义，或者包含其他的元件。&lt;/p&gt;

    &lt;p&gt;特别需要注意是这里的线网定义（如 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;clk@0&lt;/code&gt;，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;rst@1&lt;/code&gt;）。这里，&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;@&lt;/code&gt; 符号之前的字符串是他们在源码中的名字，而 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;@&lt;/code&gt; 符号之后的数字是它们在 Nxon 中的唯一标识符。由于在 Nxon 表示中消去了源码中的作用域信息，可能会出现重名的情况，因此需要用唯一标识符来区分。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;关于 Nxon 表示的详细说明，参见 NetX 标准手册。&lt;/p&gt;

&lt;h3 id=&quot;仿真测试&quot;&gt;仿真测试&lt;/h3&gt;

&lt;p&gt;NetX 提供了 C++ 实现的仿真库 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;nxsim&lt;/code&gt;，可以用它来对 Nxon 文件进行仿真测试。&lt;/p&gt;

&lt;p&gt;下面展示了项目中 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;main.cpp&lt;/code&gt; 的部分内容，该文件提供了仿真所需的测试激励&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;nxsim/circuit.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;nxsim/circuit_parser.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Memory&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nxon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse_circuit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlohmann&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr_mem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_mem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update_by_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rst&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip_by_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;clk&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_by_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;data[10]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00c0ffee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&amp;gt; &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\033&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[32mPassed!&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\033&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[0m&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;passed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&amp;gt; &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\033&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[31mFailed!&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\033&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[0m&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可见，这里&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;我们从标准输入读取 Nxon 文件的内容，并解析为一个电路上下文 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;ctx&lt;/code&gt;。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;对于每个测试用例，我们创建了两个内存对象 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;instr_mem&lt;/code&gt; 和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;data_mem&lt;/code&gt;，分别模拟指令存储器和数据存储器。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;我们通过 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;ctx.get_by_name&lt;/code&gt;， &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;ctx.update_by_name&lt;/code&gt; 以及 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;ctx.flip_by_name&lt;/code&gt; 等方法访存当前电路状态。注意，仿真器只允许对顶层模块的输入信号进行修改，但可以对任意电路中的信号进行读取。&lt;/p&gt;

    &lt;p&gt;如遇到重名等问题难以直接访问时，可以参照 Nxon 文件，通过 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;ctx.circuit.get_value(417)&lt;/code&gt; 的方式，通过 Nxon 中的唯一标识符获取电路状态。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;除此之外，仿真器还支持导出波形图、检测用例覆盖率等功能。细节见 &lt;a href=&quot;https://github.com/pascal-lab/NetX&quot;&gt;NetX 仓库&lt;/a&gt; 中提供的文档。&lt;/p&gt;

&lt;p&gt;编写了 C++ 测试激励之后，就可以使用下面的命令编译并运行测试：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;c++ &lt;span class=&quot;nt&quot;&gt;-O3&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ffast-math&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;c++23 &lt;span class=&quot;nt&quot;&gt;-flto&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; risc_test main.cpp &lt;span class=&quot;nt&quot;&gt;-lnxsim&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;-O3 -ffast-math&lt;/code&gt; 是编译器的优化选项，开启了高级别的优化以保证仿真速度&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;-std=c++23&lt;/code&gt; 指定了 C++ 的标准版本&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;-flto&lt;/code&gt; 开启了链接时优化选项。由于 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;nxsim&lt;/code&gt; 被编译为一个静态链接库，开启该选项可以让编译器在链接时根据当前测试激励编写情况作进一步的优化。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-text highlighter-rouge&quot;&gt;-lnxsim&lt;/code&gt; 指定了链接时需要链接的库，即 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;nxsim&lt;/code&gt; 仿真库。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;此时，将编译出的项目 Nxon 文件输入可执行文件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;risc_test&lt;/code&gt; 即可。比如可以通过管道&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nx compile _netx.toml &lt;span class=&quot;nt&quot;&gt;--top&lt;/span&gt; CORE &lt;span class=&quot;nt&quot;&gt;--minimal&lt;/span&gt; | ./risc_test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;进行仿真测试。下面展示了运行的输出&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Running test case: &quot;sra.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;slti.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;bne.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;bgeu.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;lhu.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;lbu.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;lw.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;sltu.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;lb.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;srli.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;beq.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;jal.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;sw.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;xori.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;addi.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;lui.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;blt.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;lh.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;srl.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;slli.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;simple.hex&quot; -&amp;gt; Passed!
Running test case: &quot;slt.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;ori.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;bltu.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;add.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;xor.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;sh.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;and.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;andi.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;sub.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;sltiu.hex&quot;  -&amp;gt; Passed!
Running test case: &quot;srai.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;or.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;bge.hex&quot;    -&amp;gt; Passed!
Running test case: &quot;auipc.hex&quot;  -&amp;gt; Passed!
Running test case: &quot;jalr.hex&quot;   -&amp;gt; Passed!
Running test case: &quot;sb.hex&quot;     -&amp;gt; Passed!
Running test case: &quot;sll.hex&quot;    -&amp;gt; Passed!
Passed 38/38 test cases
Elapsed time: 1.42926s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;电路可视化&quot;&gt;电路可视化&lt;/h3&gt;

&lt;p&gt;NetX 还提供了一个可视化工具，它接受编译产生的 Nxon 文件，并且生成电路的图形化表示。&lt;/p&gt;

&lt;p&gt;目前，可视化工具基于 &lt;a href=&quot;https://graphviz.org/&quot;&gt;Graphviz&lt;/a&gt; 实现，使用下面的命令生成电路图：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nx visualize _netx.toml &lt;span class=&quot;nt&quot;&gt;--top&lt;/span&gt; CORE &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; vis.dot &lt;span class=&quot;nt&quot;&gt;--depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2
dot &lt;span class=&quot;nt&quot;&gt;-T&lt;/span&gt; pdf vis.dot &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; vis.pdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;--depth=2&lt;/code&gt; 规定了电路图展开的深度，我们从顶层元件 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;CORE&lt;/code&gt; 出发只展开两层，得到：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://naiiren.github.io/assets/cpu_view.png&quot; alt=&quot;cpu_view&quot; /&gt;&lt;/p&gt;

&lt;p&gt;从图上依稀可以分辨出：整个图片最左侧比较杂乱的部分是指令译码器；右侧排线整齐的部分是寄存器堆；其余部分则是 ALU、立即数选择器等元件。不过，现在导出的电路图还有很大的改进空间：我们使用的 Graphviz 布局引擎不能很好地处理元件较多的电路，我们正在积极寻找改进方案。&lt;/p&gt;

&lt;h3 id=&quot;fpga-部署&quot;&gt;FPGA 部署&lt;/h3&gt;

&lt;p&gt;在完成了上面的仿真测试后，我们就可以将设计部署到 FPGA 上了。&lt;/p&gt;

&lt;p&gt;这里我们采用了一块 Terasic DE2-115 开发版，它需要与官方提供的 System Builder 与 Intel Quartus Prime 软件配合使用。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;首先使用 System Builder 生成空的 Verilog 项目，该项目只包含了与 FPGA 板载资源交互的相关接口。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;再使用 Intel Quartus Prime 创建与板载内存相关的 IP 核接口。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;在完成上述准备后，我们只需将这些生成的接口与 NetX 设计相连即可。&lt;/p&gt;

    &lt;p&gt;使用下面的编译指令&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nx dump _netx.toml &lt;span class=&quot;nt&quot;&gt;--top&lt;/span&gt; CORE &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; core.v
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;将 NetX 源码首先编译到 Nxon 底层表示，之后将底层表示翻译到 Verilog。注意，这里的 Verilog 表示已接近网表级表示，避开了原生 Verilog 中的各种行为级建模，仅仅保留了 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;always @(posedge clk)&lt;/code&gt; 结构表示时序逻辑和 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;assign lhs = rhs&lt;/code&gt; 表示组合逻辑。&lt;/p&gt;

    &lt;p&gt;相连得到&lt;/p&gt;

    &lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;wire&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;wire&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imem_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;wire&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;wire&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;wire&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_we&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assign&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  
&lt;span class=&quot;n&quot;&gt;mod_0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;clk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imem_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;dmem_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dmem_we&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;HEX0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HEX7&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  
&lt;span class=&quot;k&quot;&gt;assign&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LEDR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;ram_a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_mem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;byteena_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dmem_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dmem_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rdaddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dmem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rdclock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wraddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dmem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrclock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wren&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dmem_we&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dmem_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  
&lt;span class=&quot;n&quot;&gt;ram_b&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr_mem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imem_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mb&quot;&gt;32&apos;b0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wren&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mb&quot;&gt;1&apos;b0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imem_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/*...*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;完成上述代码准备后，使用 Quartus 综合并将电路烧录到 FPGA 开发版上。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;最后，使用 Quartus 的板载内存编辑器，将测试用例写入板载内存，即可进行测试。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;下面展示了该项目成功执行测试程序，将 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;0x00C0FFEE&lt;/code&gt; 写入 &lt;code class=&quot;language-text highlighter-rouge&quot;&gt;x10&lt;/code&gt; 寄存器，标志测试通过的图片（为方便展示，这里还包括了 CPU 设计以外的七段数码管等）：&lt;/p&gt;

&lt;center&gt;
&lt;img src=&quot;https://naiiren.github.io/assets/FPGA.jpg&quot; alt=&quot;FPGA&quot; style=&quot;zoom: 20%;&quot; /&gt;
&lt;/center&gt;
</description>
        <pubDate>Tue, 01 Jul 2025 08:25:00 +0000</pubDate>
        <link>https://naiiren.github.io/articles/netx-tutorial/</link>
        <guid isPermaLink="true">https://naiiren.github.io/articles/netx-tutorial/</guid>
        
        
      </item>
    
  </channel>
</rss>
