如何在“Nand to Tetris”中设置 ALU 的输出标志 课程?
虽然我给这个作业贴了标签,但它实际上是我自己免费做的一门课程。 不管怎样,这门课程叫做“从 Nand 到俄罗斯方块”,我希望这里有人看过或参加过这门课程,这样我就能得到一些帮助。 我正处于使用提供的 hdl 语言构建 ALU 的阶段。 我的问题是我无法正确编译我的芯片。 当我尝试设置 ALU 的输出标志时出现错误。 我相信问题是我无法为任何中间变量添加下标,因为当我尝试根据某些随机变量(例如输入标志)将标志设置为 true 或 false 时,我没有收到错误。 我知道问题不在于我尝试使用的芯片,因为我使用的是所有内置芯片。
这是到目前为止我的 ALU 芯片:
/**
* The ALU. Computes a pre-defined set of functions out = f(x,y)
* where x and y are two 16-bit inputs. The function f is selected
* by a set of 6 control bits denoted zx, nx, zy, ny, f, no.
* The ALU operation can be described using the following pseudocode:
* if zx=1 set x = 0 // 16-bit zero constant
* if nx=1 set x = !x // Bit-wise negation
* if zy=1 set y = 0 // 16-bit zero constant
* if ny=1 set y = !y // Bit-wise negation
* if f=1 set out = x + y // Integer 2's complement addition
* else set out = x & y // Bit-wise And
* if no=1 set out = !out // Bit-wise negation
*
* In addition to computing out, the ALU computes two 1-bit outputs:
* if out=0 set zr = 1 else zr = 0 // 16-bit equality comparison
* if out<0 set ng = 1 else ng = 0 // 2's complement comparison
*/
CHIP ALU {
IN // 16-bit inputs:
x[16], y[16],
// Control bits:
zx, // Zero the x input
nx, // Negate the x input
zy, // Zero the y input
ny, // Negate the y input
f, // Function code: 1 for add, 0 for and
no; // Negate the out output
OUT // 16-bit output
out[16],
// ALU output flags
zr, // 1 if out=0, 0 otherwise
ng; // 1 if out<0, 0 otherwise
PARTS:
// Zero the x input
Mux16( a=x, b=false, sel=zx, out=x2 );
// Zero the y input
Mux16( a=y, b=false, sel=zy, out=y2 );
// Negate the x input
Not16( in=x, out=notx );
Mux16( a=x, b=notx, sel=nx, out=x3 );
// Negate the y input
Not16( in=y, out=noty );
Mux16( a=y, b=noty, sel=ny, out=y3 );
// Perform f
Add16( a=x3, b=y3, out=addout );
And16( a=x3, b=y3, out=andout );
Mux16( a=andout, b=addout, sel=f, out=preout );
// Negate the output
Not16( in=preout, out=notpreout );
Mux16( a=preout, b=notpreout, sel=no, out=out );
// zr flag
Or8way( in=out[0..7], out=zr1 ); // PROBLEM SHOWS UP HERE
Or8way( in=out[8..15], out=zr2 );
Or( a=zr1, b=zr2, out=zr );
// ng flag
Not( in=out[15], out=ng );
}
因此,当我尝试将“out”的下标版本发送到 Or8Way 芯片时,问题就出现了。 我尝试使用与“out”不同的变量,但遇到了同样的问题。 然后我读到您无法为中间变量添加下标。 我想也许如果我将中间变量发送到其他芯片,并且该芯片对其进行下标,它可以解决问题,但它有相同的错误。 不幸的是,我只是想不出一种方法来设置 zr 和 ng 标志而不用下标某些中间变量,所以我真的被困住了!
只是想让你知道,如果我用以下内容替换有问题的行,它将编译(但不会给出正确的结果,因为我只是使用一些随机输入):
// zr flag
Not( in=zx, out=zr );
// ng flag
Not( in=zx, out=ng );
有人有任何想法吗?
编辑:这是本课程书籍的附录 指定 hdl 的工作方式。 具体请看第 5 节,其中讨论了总线并说:“内部引脚(如上面的 v)可能没有下标”。
编辑:这是我得到的确切错误:“第 68 行,无法将门的输出引脚连接到部件”。 不过,错误消息有点令人困惑,因为这似乎不是真正的问题。 如果我只是替换“Or8way( in=out[0..7], out=zr1 );” 与“Or8way(in = false,out = zr1);” 它不会生成此错误,这就是导致我在附录中查找并发现 out 变量(因为它是作为中间派生的)无法添加下标的原因。
Although I tagged this homework, it is actually for a course which I am doing on my own for free. Anyway, the course is called "From Nand to Tetris" and I'm hoping someone here has seen or taken the course so I can get some help. I am at the stage where I am building the ALU with the supplied hdl language. My problem is that I can't get my chip to compile properly. I am getting errors when I try to set the output flags for the ALU. I believe the problem is that I can't subscript any intermediate variable, since when I just try setting the flags to true or false based on some random variable (say an input flag), I do not get the errors. I know the problem is not with the chips I am trying to use since I am using all builtin chips.
Here is my ALU chip so far:
/**
* The ALU. Computes a pre-defined set of functions out = f(x,y)
* where x and y are two 16-bit inputs. The function f is selected
* by a set of 6 control bits denoted zx, nx, zy, ny, f, no.
* The ALU operation can be described using the following pseudocode:
* if zx=1 set x = 0 // 16-bit zero constant
* if nx=1 set x = !x // Bit-wise negation
* if zy=1 set y = 0 // 16-bit zero constant
* if ny=1 set y = !y // Bit-wise negation
* if f=1 set out = x + y // Integer 2's complement addition
* else set out = x & y // Bit-wise And
* if no=1 set out = !out // Bit-wise negation
*
* In addition to computing out, the ALU computes two 1-bit outputs:
* if out=0 set zr = 1 else zr = 0 // 16-bit equality comparison
* if out<0 set ng = 1 else ng = 0 // 2's complement comparison
*/
CHIP ALU {
IN // 16-bit inputs:
x[16], y[16],
// Control bits:
zx, // Zero the x input
nx, // Negate the x input
zy, // Zero the y input
ny, // Negate the y input
f, // Function code: 1 for add, 0 for and
no; // Negate the out output
OUT // 16-bit output
out[16],
// ALU output flags
zr, // 1 if out=0, 0 otherwise
ng; // 1 if out<0, 0 otherwise
PARTS:
// Zero the x input
Mux16( a=x, b=false, sel=zx, out=x2 );
// Zero the y input
Mux16( a=y, b=false, sel=zy, out=y2 );
// Negate the x input
Not16( in=x, out=notx );
Mux16( a=x, b=notx, sel=nx, out=x3 );
// Negate the y input
Not16( in=y, out=noty );
Mux16( a=y, b=noty, sel=ny, out=y3 );
// Perform f
Add16( a=x3, b=y3, out=addout );
And16( a=x3, b=y3, out=andout );
Mux16( a=andout, b=addout, sel=f, out=preout );
// Negate the output
Not16( in=preout, out=notpreout );
Mux16( a=preout, b=notpreout, sel=no, out=out );
// zr flag
Or8way( in=out[0..7], out=zr1 ); // PROBLEM SHOWS UP HERE
Or8way( in=out[8..15], out=zr2 );
Or( a=zr1, b=zr2, out=zr );
// ng flag
Not( in=out[15], out=ng );
}
So the problem shows up when I am trying to send a subscripted version of 'out' to the Or8Way chip. I've tried using a different variable than 'out', but with the same problem. Then I read that you are not able to subscript intermediate variables. I thought maybe if I sent the intermediate variable to some other chip, and that chip subscripted it, it would solve the problem, but it has the same error. Unfortunately I just can't think of a way to set the zr and ng flags without subscripting some intermediate variable, so I'm really stuck!
Just so you know, if I replace the problematic lines with the following, it will compile (but not give the right results since I'm just using some random input):
// zr flag
Not( in=zx, out=zr );
// ng flag
Not( in=zx, out=ng );
Anyone have any ideas?
Edit: Here is the appendix of the book for the course which specifies how the hdl works. Specifically look at section 5 which talks about buses and says: "An internal pin (like v above) may not be subscripted".
Edit: Here is the exact error I get: "Line 68, Can't connect gate's output pin to part". The error message is sort of confusing though, since that does not seem to be the actual problem. If I just replace "Or8way( in=out[0..7], out=zr1 );" with "Or8way( in=false, out=zr1 );" it will not generate this error, which is what lead me to look up in the appendix and find that the out variable, since it was derived as intermediate, could not be subscripted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于其他感兴趣的人,模拟器支持的解决方案是使用多个输出
就像是:
For anyone else interested, the solution the emulator supports is to use multiple outputs
Something like:
这就是我做 ALU 的方法:
This is how I did the ALU:
Pax 建议的解决方案是使用中间变量作为另一个芯片(例如 Or16Way)的输入。 这是我修复问题并调试后的代码:
The solution as Pax suggested was to use an intermediate variable as input to another chip, such as Or16Way. Here is the code after I fixed the problem and debugged:
您尝试过吗:
我不知道这是否可行,但查看此文档似乎有意义 此处。
对于使用
out
作为变量名,我也会三思而后行,因为试图找出它与关键字out
之间的区别会让人感到困惑(如“out =...
”)。编辑后,如果您无法为中间值添加下标,那么您似乎必须实现一个单独的“芯片”,例如
IsZero16
,它将采用 16 位值作为输入(您的中间out
)并返回一位,指示其为零,您可以将其加载到zr
中。 或者您可以制作一个IsZero8
芯片,但您必须将其称为两个阶段,就像您当前使用Or8Way
所做的那样。这似乎是一个有效的解决方案,因为您可以为芯片的输入值添加下标。
而且,只要查看错误,这可能是与您建议的问题不同的问题。 “无法将门的输出引脚连接到部件”这句话对我来说意味着您无法将来自输出参数的信号连接回芯片处理区域。 从电气角度来看,这是有道理的。
您可能会发现必须将输出存储到临时变量中,并使用它来设置
zr
和out
(因为一旦信号被“发送”到芯片输出引脚,它们可能不再可用)。我们可以尝试:
然后,在你的 ALU 芯片中,最后使用这个:
Have you tried:
I don't know if this will work but it seems to make sense from looking at this document here.
I'd also think twice about using
out
as a variable name since it's confusing trying to figure out the difference between that and the keywordout
(as in "out=...
").Following your edit, if you cannot subscript intermediate values, then it appears you will have to implement a separate "chip" such as
IsZero16
which will take a 16-bit value as input (your intermediateout
) and return one bit indicating its zero-ness that you can load intozr
. Or you could make anIsZero8
chip but you'd have to then call it it two stages as you're currently doing withOr8Way
.This seems like a valid solution since you can subscript the input values to a chip.
And, just looking at the error, this may be a different problem to the one you suggest. The phrase "Can't connect gate's output pin to part" would mean to me that you're unable to connect signals from the output parameter back into the chips processing area. That makes sense from an electrical point of view.
You may find you have to store the output into a temporary variable and use that to both set
zr
andout
(since once the signals have been "sent" to the chips output pins, they may no longer be available).Can we try:
and then, in your ALU chip, use this at the end:
这也配备了新芯片,但感觉更干净
Here's one also with a new chip but it feels cleaner