牛客网Verilog刷题——VL23

牛客网Verilog刷题——VL23

题目

  实现一个深度为8,位宽为4bit的ROM,数据初始化为0,2,4,6,8,10,12,14。可以通过输入地址addr,输出相应的数据data。该模块的接口信号图如下:

在这里插入图片描述

信号类型输入/输出位宽
clkwireIntput1
rst_nwireIntput1
addrwireIntput8
datawireOutput4

  其中,输入的8比特位宽的地址和输出的4比特位宽的数据均为无符号数。

答案

  需要注意的是,题目中说数据初始化为0,2,4,6,8,10,12,14,是对前8个地址初始化为该数值,即第0~7个地址。

`timescale 1ns/1ns
module rom(
	input clk,
	input rst_n,
	input [7:0]addr,
	
	output [3:0]data
);

reg [3:0]   rom_data    [7:0];
always @(posedge clk or negedge rst_n)
    if(!rst_n) begin
        rom_data[0] <= 4'd0;
        rom_data[1] <= 4'd2;
        rom_data[2] <= 4'd4;
        rom_data[3] <= 4'd6;
        rom_data[4] <= 4'd8;
        rom_data[5] <= 4'd10;
        rom_data[6] <= 4'd12;
        rom_data[7] <= 4'd14;
    end
    else begin
        rom_data[0] <= 4'd0;
        rom_data[1] <= 4'd2;
        rom_data[2] <= 4'd4;
        rom_data[3] <= 4'd6;
        rom_data[4] <= 4'd8;
        rom_data[5] <= 4'd10;
        rom_data[6] <= 4'd12;
        rom_data[7] <= 4'd14;   
    end
assign data = rom_data[addr];

endmodule
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值