当前位置: 首页 > news >正文

nju实验二 译码器和编码器

实验二 译码器和编码器

2-4译码器

.

├── constr
│ └── top.nxdc
├── csrc
│ └── test_our.cpp
├── Makefile
├── vsrc
│ └── top.v
├── top.v
├── vlt_dump.vcd
├── test_our.cpp
├── obj_dir
└── dump.vcd

├── constr

└── top.nxdc

top=topy (LD3, LD2, LD1, LD0)
x (SW1, SW0)en (SW2)

├── csrc

└── test_our.cpp

#include<nvboard.h>
#include<Vtop.h>static TOP_NAME dut;void nvboard_bind_all_pins(TOP_NAME* top);static void single_cycle(){dut.eval();
}int main(){nvboard_bind_all_pins(&dut);nvboard_init();while(1){nvboard_update();single_cycle();}}

├── test_our.cpp

#include "verilated.h"
#include "verilated_vcd_c.h"
#include "obj_dir/Vtop.h"VerilatedContext* contextp = NULL;
VerilatedVcdC* tfp = NULL;static Vtop* top;void step_and_dump_wave(){top->eval();contextp->timeInc(1);tfp->dump(contextp->time());
}
void sim_init(){contextp = new VerilatedContext;tfp = new VerilatedVcdC;top = new Vtop;contextp->traceEverOn(true);top->trace(tfp, 0);tfp->open("dump.vcd");
}void sim_exit(){step_and_dump_wave();tfp->close();
}int main() {sim_init();top->en = 0b0;  top->x = 0b00;  step_and_dump_wave();top->x = 0b01;  step_and_dump_wave();top->x = 0b10;  step_and_dump_wave();top->x = 0b11;  step_and_dump_wave();top->en = 0b1;  top->x = 0b00;  step_and_dump_wave();top->x = 0b01;  step_and_dump_wave();top->x = 0b10;  step_and_dump_wave();top->x = 0b11;  step_and_dump_wave();sim_exit();
}

├── vsrc

└── top.v

module top(x,en,y);input  [1:0] x;input  en;output reg [3:0]y;always @(x or en)if (en)begincase (x)2'd0 : y = 4'b0001;2'd1 : y = 4'b0010;2'd2 : y = 4'b0100;2'd3 : y = 4'b1000;endcaseendelse  y = 4'b0000;endmodule

├── top.v

module top(x,en,y);input  [1:0] x;input  en;output reg [3:0]y;always @(x or en)if (en)begincase (x)2'd0 : y = 4'b0001;2'd1 : y = 4'b0010;2'd2 : y = 4'b0100;2'd3 : y = 4'b1000;endcaseendelse  y = 4'b0000;endmodule

波形仿真

(1)编译
verilator -Wall --trace -cc top.v --exe main.cpp
(2)生成可执行文件make -C obj_dir -f Vtop.mk Vtop
(3)生成波形
./obj_dir/Vtop
(4)查看波形
gtkwave dump.vcd

接入NVBoard

make
cd build
./top

4-2编码器

.

├── constr
│ └── top.nxdc
├── csrc
│ └── test_our.cpp
├── Makefile
├── vsrc
│ └── top.v
├── top.v
├── vlt_dump.vcd
├── test_our.cpp
├── obj_dir
└── dump.vcd

├── constr

└── top.nxdc

top=topy (LD1, LD0)
x (SW3, SW2, SW1, SW0)en (SW4)

├── csrc

└── test_our.cpp

#include<nvboard.h>
#include<Vtop.h>static TOP_NAME dut;void nvboard_bind_all_pins(TOP_NAME* top);static void single_cycle(){dut.eval();
}int main(){nvboard_bind_all_pins(&dut);nvboard_init();while(1){nvboard_update();single_cycle();}}

├── test_our.cpp

#include "verilated.h"
#include "verilated_vcd_c.h"
#include "obj_dir/Vtop.h"VerilatedContext* contextp = NULL;
VerilatedVcdC* tfp = NULL;static Vtop* top;void step_and_dump_wave(){top->eval();contextp->timeInc(1);tfp->dump(contextp->time());
}
void sim_init(){contextp = new VerilatedContext;tfp = new VerilatedVcdC;top = new Vtop;contextp->traceEverOn(true);top->trace(tfp, 0);tfp->open("dump.vcd");
}void sim_exit(){step_and_dump_wave();tfp->close();
}int main() {sim_init();top->en=0b0; top->x =0b0000; step_and_dump_wave();top->x =0b0001; step_and_dump_wave();top->x =0b0010; step_and_dump_wave();top->x =0b0100; step_and_dump_wave();top->x =0b1000; step_and_dump_wave();top->en=0b1; top->x =0b0000; step_and_dump_wave();top->x =0b0001; step_and_dump_wave();top->x =0b0010; step_and_dump_wave();top->x =0b0100; step_and_dump_wave();top->x =0b1000; step_and_dump_wave();sim_exit();
}

├── vsrc

└── top.v

module top(x,en,y);input  [3:0] x;input  en;output reg [1:0]y;always @(x or en)if (en)begincase (x)4'b0001 : y = 2'd0;4'b0010 : y = 2'd1;4'b0100 : y = 2'd2;4'b1000 : y = 2'd3;default : y = 2'd0;endcaseendelse  y = 2'd0;endmodule

├── top.v

module top(x,en,y);input  [3:0] x;input  en;output reg [1:0]y;always @(x or en)if (en)begincase (x)4'b0001 : y = 2'd0;4'b0010 : y = 2'd1;4'b0100 : y = 2'd2;4'b1000 : y = 2'd3;default : y = 2'd0;endcaseendelse  y = 2'd0;endmodule

波形仿真

(1)编译
verilator -Wall --trace -cc top.v --exe main.cpp
(2)生成可执行文件make -C obj_dir -f Vtop.mk Vtop
(3)生成波形
./obj_dir/Vtop
(4)查看波形
gtkwave dump.vcd

image

接入NVBoard

make
cd build
./top

image

4-2优先编码器

.

├── constr
│ └── top.nxdc
├── csrc
│ └── test_our.cpp
├── Makefile
├── vsrc
│ └── top.v
├── top.v
├── vlt_dump.vcd
├── test_our.cpp
├── obj_dir
└── dump.vcd

├── constr

└── top.nxdc

top=topy (LD1, LD0)
x (SW3, SW2, SW1, SW0)en (SW4)

├── csrc

└── test_our.cpp

#include<nvboard.h>
#include<Vtop.h>static TOP_NAME dut;void nvboard_bind_all_pins(TOP_NAME* top);static void single_cycle(){dut.eval();
}int main(){nvboard_bind_all_pins(&dut);nvboard_init();while(1){nvboard_update();single_cycle();}}

├── test_our.cpp

#include "verilated.h"
#include "verilated_vcd_c.h"
#include "obj_dir/Vtop.h"VerilatedContext* contextp = NULL;
VerilatedVcdC* tfp = NULL;static Vtop* top;void step_and_dump_wave(){top->eval();contextp->timeInc(1);tfp->dump(contextp->time());
}
void sim_init(){contextp = new VerilatedContext;tfp = new VerilatedVcdC;top = new Vtop;contextp->traceEverOn(true);top->trace(tfp, 0);tfp->open("dump.vcd");
}void sim_exit(){step_and_dump_wave();tfp->close();
}int main() {sim_init();top->en=0b0; top->x =0b0000; step_and_dump_wave();top->x =0b0011; step_and_dump_wave();top->x =0b0110; step_and_dump_wave();top->x =0b0100; step_and_dump_wave();top->x =0b1000; step_and_dump_wave();top->en=0b1; top->x =0b0000; step_and_dump_wave();top->x =0b0011; step_and_dump_wave();top->x =0b0110; step_and_dump_wave();top->x =0b0100; step_and_dump_wave();top->x =0b1000; step_and_dump_wave();sim_exit();
}

├── vsrc

└── top.v

module top(x,en,y);input  [3:0] x;input  en;output reg [1:0]y;integer i;always @(x or en) beginif (en)beginy = 0;for( i = 0; i <= 3; i = i+1)if(x[i] == 1) y = i[1:0];endelse  y = 0;end
endmodule

├── top.v

module top(x,en,y);input  [3:0] x;input  en;output reg [1:0]y;integer i;always @(x or en) beginif (en)beginy = 0;for( i = 0; i <= 3; i = i+1)if(x[i] == 1) y = i[1:0];endelse  y = 0;end
endmodule

波形仿真

(1)编译
verilator -Wall --trace -cc top.v --exe main.cpp
(2)生成可执行文件make -C obj_dir -f Vtop.mk Vtop
(3)生成波形
./obj_dir/Vtop
(4)查看波形
gtkwave dump.vcd

image

接入NVBoard

make
cd build
./top

image

8-3优先编码器

.

├── constr
│ └── top.nxdc
├── csrc
│ └── test_our.cpp
├── Makefile
├── vsrc
│ └── top.v
├── top.v
├── vlt_dump.vcd
├── test_our.cpp
├── obj_dir
└── dump.vcd

├── constr

└── top.nxdc

top=topy (LD2, LD1, LD0)
valid (LD4)
x (SW7, SW6, SW5, SW4, SW3, SW2, SW1, SW0)
seg0 (SEG0A, SEG0B, SEG0C, SEG0D, SEG0E, SEG0F, SEG0G, DEC0P)
en (SW8)

├── csrc

└── test_our.cpp

#include<nvboard.h>
#include<Vtop.h>static TOP_NAME dut;void nvboard_bind_all_pins(TOP_NAME* top);static void single_cycle(){dut.eval();
}int main(){nvboard_bind_all_pins(&dut);nvboard_init();while(1){nvboard_update();single_cycle();}}

├── test_our.cpp

#include "verilated.h"
#include "verilated_vcd_c.h"
#include "obj_dir/Vtop.h"VerilatedContext* contextp = NULL;
VerilatedVcdC* tfp = NULL;static Vtop* top;void step_and_dump_wave(){top->eval();contextp->timeInc(1);tfp->dump(contextp->time());
}
void sim_init(){contextp = new VerilatedContext;tfp = new VerilatedVcdC;top = new Vtop;contextp->traceEverOn(true);top->trace(tfp, 0);tfp->open("dump.vcd");
}void sim_exit(){step_and_dump_wave();tfp->close();
}int main() {sim_init();top->en=0b0; top->x =0b00000000; step_and_dump_wave();top->x =0b00000011; step_and_dump_wave();top->x =0b00000110; step_and_dump_wave();top->x =0b00000100; step_and_dump_wave();top->x =0b00001000; step_and_dump_wave();top->x =0b00010000; step_and_dump_wave();top->x =0b00100000; step_and_dump_wave();top->x =0b01000000; step_and_dump_wave();top->x =0b10000000; step_and_dump_wave();top->en=0b1; top->x =0b00000000; step_and_dump_wave();top->x =0b00000011; step_and_dump_wave();top->x =0b00000110; step_and_dump_wave();top->x =0b00000100; step_and_dump_wave();top->x =0b00001000; step_and_dump_wave();top->x =0b00010000; step_and_dump_wave();top->x =0b00100000; step_and_dump_wave();top->x =0b01000000; step_and_dump_wave();top->x =0b10000000; step_and_dump_wave();sim_exit();
}

├── vsrc

└── top.v

module top (input [7:0] x,      // 8位输入input en,           // 使能端output [2:0] y,     // 3位编码输出output valid,       // 有效指示位output [7:0] seg0    // 七段数码管输出
);// 实例化优先编码器priority_encoder_8to3 encoder (.x(x),.en(en),.y(y),.valid(valid));// 实例化七段译码器seg7_decoder decoder (.bin_in(y),.seg0(seg0));
endmodulemodule priority_encoder_8to3 (input [7:0] x,      // 8位输入input en,           // 使能端output reg [2:0] y, // 3位编码输出output valid        // 有效指示位(至少有一个输入为1)
);always @(x or en) beginif (en) begincasez (x)8'b1???????: y = 3'b111; // 最高位优先8'b01??????: y = 3'b110;8'b001?????: y = 3'b101;8'b0001????: y = 3'b100;8'b00001???: y = 3'b011;8'b000001??: y = 3'b010;8'b0000001?: y = 3'b001;8'b00000001: y = 3'b000;default:    y = 3'b000; // 全0情况endcaseendelse beginy = 3'b000; // 使能无效时输出0endendassign valid = en && (|x); // 使能且至少有一个1时有效
endmodule

└── seg.v

module seg7_decoder (input [2:0] bin_in,    // 3位二进制输入output [7:0] seg0   // 七段数码管输出(共阳)
);reg [7:0] seg;always @(bin_in) begincase (bin_in)3'b000: seg = 8'b11111101; // 03'b001: seg = 8'b01100000; // 13'b010: seg = 8'b11011010; // 23'b011: seg = 8'b11110010; // 33'b100: seg = 8'b01100110; // 43'b101: seg = 8'b10110110; // 53'b110: seg = 8'b10111110; // 63'b111: seg = 8'b11100000; // 7default: seg = 8'b11111111; // 全灭endcaseendassign seg0 = ~seg;
endmodule

波形仿真

(1)编译
verilator -Wall --trace -cc top.v --exe main.cpp
(2)生成可执行文件make -C obj_dir -f Vtop.mk Vtop
(3)生成波形
./obj_dir/Vtop
(4)查看波形
gtkwave dump.vcd

image

接入NVBoard

make
cd build
./top

image
image

http://www.rkmt.cn/news/56726.html

相关文章:

  • 第四十六篇
  • 2025年送礼水果排行榜权威推荐,拉吾尤摩赣南脐橙荣登榜首
  • AI救星!8个写毕业论文的实用AI工具大揭秘
  • 数据血缘图在数据错误追溯中的应用指南
  • CSS基础语法 - 指南
  • 「Temp」目录
  • 高中学习机五大品牌终极横评:优缺点一览,找到最适合你的那一款!
  • 开发智联笔记项目时所遇问题(4)
  • 20251121周五日记
  • CrewAI 上手攻略:多 Agent 自动化处理复杂任务,让 AI 像员工一样分工协作
  • 开发智联笔记项目所遇问题
  • 实用指南:【记录】MAC本地微调大模型(MLX + Qwen2.5)并利用Ollama接入项目实战
  • 搜维尔科技:利用MANUS数据手套实现灵巧远程操作:对20自由度灵巧手进行控制
  • nju实验一选择器
  • AI浪潮下的新动向:协作、法律与未来工作
  • Day36:2025年10月26日,星期天,休息。
  • 成都合成树脂瓦使用寿命影响因素?成都佳英耀旺告诉你
  • Talent AI ——专家级大模型数据标注平台
  • 团队作业 3
  • for循环的详细解析for...of循环同时获取下标和data
  • [豪の算法奇妙冒险] 代码随想录算法训练营第三天 | 203-移除链表元素、707-设计链表、206-反转链表
  • 2025年11月北京/东城区/西城区/朝阳区/海淀区/丰台区/石景山区遗产继承律师,遗产咨询律所Top10专业推荐排行权威榜单
  • hspice 写了一个振幅可变的电压源,并且可以进行直流偏执
  • 实用指南:【设计模式】适配器模式(Adapter)
  • FPGA专用CLKUSR时钟引脚严重警告——Cyclone 10 GX
  • rich dataset 3D人体场景数据集
  • ICPC2025沈阳打铁日志
  • 实用指南:一文搞懂 DeepSeek API:兼容 OpenAI 接口的智能对话模型调用指南
  • 形容词Test
  • 最佳加密货币钱包选择指南:企业级安全与功能全解析