求助:用两个8位寄存器实现Nand2Tetris课程的16位PC
Hey there! Let’s work through this Nand2Tetris problem—using two 8-bit Registers to mimic a 16-bit PC is totally doable, but your current code has a few key issues that are preventing it from working correctly. Let’s break down what’s wrong and how to fix it.
What’s Off in Your Current Code?
Your setup has both registers hooked to the full 16-bit cout input, which means each register is trying to process all 16 bits instead of their assigned 8-bit slice. Additionally, sharing a single feedback signal between both registers breaks the state-retention logic, and you need to ensure both registers load new values at the exact same time.
Corrected Implementation
Here’s how to adjust your code to make the two 8-bit registers work together as a single 16-bit PC:
// Low 8-bit Register: manages bits 0-7 of the 16-bit PC Register(in=cout[0..7], out=pc_out[0..7], out=low_feedback, load=pc_load); // High 8-bit Register: manages bits 8-15 of the 16-bit PC Register(in=cout[8..15], out=pc_out[8..15], out=high_feedback, load=pc_load);
Key Changes Explained
- Split the 16-bit input: Instead of feeding the full
coutsignal to both registers, split it into two 8-bit slices. The low register getscout[0..7](the least significant bits), and the high register getscout[8..15](the most significant bits). - Synchronized load control: Use a single
pc_loadsignal for both registers. This ensures that when you need to update the PC, both the high and low 8-bit sections update simultaneously—critical for maintaining a valid 16-bit value. - Per-register feedback: Each register uses its own output as feedback (
low_feedbackfor the low register,high_feedbackfor the high register). This lets each register retain its 8-bit state independently whenpc_loadis false, combining to keep the full 16-bit PC value intact.
Bonus: Adding PC Increment Logic (If Needed)
If your PC needs to increment (a common requirement in Nand2Tetris), feed the combined pc_out[0..15] into a 16-bit incrementer, then pass that incremented value to the cout input of your registers. This way, when pc_load is triggered, the PC updates to the incremented value.
内容的提问来源于stack exchange,提问作者Gaben

