verilog - 14 位二进制到 4 位(16 位)bcd
问题描述
我正在尝试将 14 位二进制转换为 4 位 bcd。但它不能很好地工作。给我一些想法
这是我的代码
module binary2BCD(
input [13:0] binary,
output reg [15:0] o
);
reg [29:0]shift=0;
integer i;
always@(*) begin
shift = 30'b0;
shift[13:0] = binary;
for (i=0; i<14; i=i+1) begin
if (shift[17:14] > 4'b0100)
shift[17:14] = shift[17:14] + 4'b0011;
if (shift[21:18] > 4'b0100)
shift[21:18] = shift[17:14] + 4'b0011;
if (shift[25:22] > 4'b0100)
shift[25:22] = shift[17:14] + 4'b0011;
if (shift[29:26] > 4'b0100)
shift[29:26] = shift[17:14] + 4'b0011;
shift = shift << 1;
end
o= {shift[29:26], shift[25:22], shift[21:18], shift[17:14]};
end
endmodule
解决方案
这看起来很不错。我认为唯一的问题是您增加了错误的 BCD 数字。
if (shift[21:18] > 4'b0100)
shift[21:18] = shift[17:14] + 4'b0011;
^^^^^ should be 21:18
推荐阅读
- vim - 为什么我可以在单行中声明 if 而不是 Vimscript 中的函数?
- r - 创建一个逻辑以在具有两个元素的列表中捕获两种类型的不等式
- linux - 如何通过按取消按钮关闭 zenity 窗口?
- python - 如何让 Sympy(在 python 中)插入函数的给定值?
- c++ - 如何使用范围枚举模板参数设置数组别名的长度而不进行强制转换
- c - 为什么我的“c”变量在 for 循环中到达第三个循环时呈指数增长?
- javascript - 使用不和谐 js 删除用户消息中的特定嵌入(或显示正确存储的嵌入)
- c++ - 不知道如何解决:“。”之前的预期主表达式 令牌
- react-native - 以高性能的方式渲染 FlatList 的视频
- python - TF Keras 模型输入上的低通滤波器