HDLbits答案更新系列1(1 Getting Started 2 Verilog Language 2.1 Basics 2.2 Vectors)

本文详细介绍了Verilog语言的基础知识,包括简单线、四种基本逻辑门的实现,以及向量的声明、使用和操作。通过具体实例讲解了如何在Verilog中进行向量的位操作、位运算、四输入门设计、向量拼接和反转等高级技巧。

想深耕嵌入式?这个专辑值得收藏

MCU、FPGA、工控、传感器一站式学习,实战项目直接抄

目录

前言

1 Getting Started

1.1 Getting Started(Step one)

1.2 Output Zero(Zero)

2 Verilog Language

2.1 Basics

2.1.1 Simple wire(wire)

2.1.2 Four wires(wire4)

2.1.3 Inverter(Notgate)

2.1.4 AND gate(Andgate)

2.1.5 NOR gate (Norgate)

2.1.6 XNOR gate(Xnorgate)

2.1.7 Declaring wires(Wire decl)

2.1.8 7458 chip(7458)

2.2 Vectors

2.2.1 Vectors(Vector0)

2.2.2 Vectors in more detail(Vector1)

2.2.3 Vector part select(Vector2)

2.2.4 Bitwise operators(Vectorgates)

2.2.5 Four-input gates(Gates4)

2.2.6 Vector concatenation operator(Vector3)

2.2.7 Vector reversal 1(Vectorr)

2.2.8 Replication operation(Vector4)

2.2.9 More replication(Vector5)

结语

HDLbits网站链接


前言

HDLbits的题目已经做完好久了,为了更好的巩固学习内容,从今天开始此博客每天更新系列答案,有些题目可能有多种方法, 博主会把自己的方法都写出来。如果有部分题目难一些的话,博主会给出自己的说明,同学们如果发现代码有错误的地方,欢迎大家留言指出来,博主会尽快改正。希望能和大家一起学习,一起进步。

1 Getting Started

1.1 Getting Started(Step one)

module top_module( output one );

// Insert your code here
    assign one = 1'b1;

endmodule

1.2 Output Zero(Zero)

module top_module(
    output zero
);// Module body starts after semicolon
    
    assign zero = 1'b0;

endmodule

2 Verilog Language

2.1 Basics

2.1.1 Simple wire(wire)

module top_module( input in, output out );
    
    assign out = in;
    
endmodule

2.1.2 Four wires(wire4)

module top_module( 
    input a,b,c,
    output w,x,y,z );
    
    assign w = a;
    assign x = b;
    assign y = b;
    assign z = c;

endmodule

2.1.3 Inverter(Notgate)

module top_module( input in, output out );
    
    assign out = ~in;

    //second way
    //not(out, in);
    
endmodule

2.1.4 AND gate(Andgate)

module top_module( 
    input a, 
    input b, 
    output out );
    
    assign out = a & b;  

    //second way
    //and(out, a, b);

endmodule

2.1.5 NOR gate (Norgate)

module top_module( 
    input a, 
    input b, 
    output out );
    
    assign out = ~(a | b);

endmodule

2.1.6 XNOR gate(Xnorgate)

module top_module( 
    input a, 
    input b, 
    output out );
    
    assign out = a ~^ b;

    //second way
    //assign out = a ^~ b;

    //third way
    //assign out = ~(a ^ b);

endmodule

2.1.7 Declaring wires(Wire decl)

module top_module(
    input a,
    input b,
    input c,
    input d,
    output out,
    output out_n   ); 
    
    wire w1;
    wire w2;
    
    assign w1 = a & b;
    assign w2 = c & d;
    assign out = w1 | w2;
    assign out_n = ~(w1 | w2);
    
    //second way
    //assign out = a & b | c & d;
    //assign out_n = ~(a & b | c & d);

endmodule

2.1.8 7458 chip(7458)

module top_module ( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );
    
    assign p1y = p1a & p1b & p1c | p1d & p1e & p1f;
    assign p2y = p2a & p2b | p2c & p2d;

endmodule

2.2 Vectors

2.2.1 Vectors(Vector0)

module top_module ( 
    input wire [2:0] vec,
    output wire [2:0] outv,
    output wire o2,
    output wire o1,
    output wire o0  ); // Module body starts after module declaration
    
    assign outv = vec;
    assign o2 = vec[2];
    assign o1 = vec[1];
    assign o0 = vec[0];
    
    //second way
    //assign outv = vec;
    //assign {o2, o1, o0} = vec;

endmodule

2.2.2 Vectors in more detail(Vector1)

module top_module( 
    input wire [15:0] in,
    output wire [7:0] out_hi,
    output wire [7:0] out_lo );
    
    assign out_hi = in[15:8];
    assign out_lo = in[7:0];
    
    //second way
    //assign {out_hi, out_lo} = in;

endmodule

2.2.3 Vector part select(Vector2)

module top_module( 
    input [31:0] in,
    output [31:0] out );//

    assign out = {in[7:0], in[15:8], in[23:16], in[31:24]};
    
    //second way
    //assign out[31:24] = in[7:0];	
    //assign out[23:16] = in[15:8];	
    //assign out[15:8] = in[23:16];	
    //assign out[7:0] = in[31:24];	

endmodule

2.2.4 Bitwise operators(Vectorgates)

module top_module( 
    input [2:0] a,
    input [2:0] b,
    output [2:0] out_or_bitwise,
    output out_or_logical,
    output [5:0] out_not
);
    
    assign out_or_bitwise = a | b;
    assign out_or_logical = a || b;
    assign out_not[2:0] = ~a;
    assign out_not[5:3] = ~b;

endmodule

2.2.5 Four-input gates(Gates4)

module top_module( 
    input [3:0] in,
    output out_and,
    output out_or,
    output out_xor  
);

    assign out_and = & in;
    assign out_or = | in;
    assign out_xor = ^ in;
    
endmodule

2.2.6 Vector concatenation operator(Vector3)

module top_module (
    input [4:0] a, b, c, d, e, f,
    output [7:0] w, x, y, z );//
    
    assign {w, x, y, z} = {a, b, c, d, e, f, 2'b11};
    
endmodule

2.2.7 Vector reversal 1(Vectorr)

module top_module( 
    input [7:0] in,
    output [7:0] out
);
    
    generate
        genvar i;
        for(i = 0; i <= 7; i = i + 1)begin:conv
            assign out[i] = in[7-i];
        end
    endgenerate
    
    //second way
    /*
    integer i;
    always@(*)begin
        for(i = 0; i <= 7; i = i + 1)begin
            out[i] = in[7-i];
        end
    end
    */
    
    //third way
    //assign out = {in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]};

endmodule

2.2.8 Replication operation(Vector4)

module top_module (
    input [7:0] in,
    output [31:0] out );//

    assign out = {{24{in[7]}}, in};
    
    //second way
    //assign out = in[7] ? {{24{1'b1}}, in} : {{24{1'b0}}, in};

endmodule

2.2.9 More replication(Vector5)

module top_module (
    input a, b, c, d, e,
    output [24:0] out );//
    
    assign out = {{5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}}} ~^ {5{a, b, c, d, e}};
    
    // second way
    /*
    wire [24:0] in1;
    wire [24:0] in2;
    
    assign in1 = {{5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}}};
    assign in2 = {5{a, b, c, d, e}};
    assign out = in1 ~^ in2;
    */
    
    //third way
    /*
    assign out = {{a ~^ a}, {a ~^ b}, {a ~^ c}, {a ~^ d}, {a ~^ e},
                  {b ~^ a}, {b ~^ b}, {b ~^ c}, {b ~^ d}, {b ~^ e},
                  {c ~^ a}, {c ~^ b}, {c ~^ c}, {c ~^ d}, {c ~^ e},
                  {d ~^ a}, {d ~^ b}, {d ~^ c}, {d ~^ d}, {d ~^ e},
                  {e ~^ a}, {e ~^ b}, {e ~^ c}, {e ~^ d}, {e ~^ e}};
    */

endmodule

结语

今天先更新这三个小节吧,开始的内容相对基础,适合用来巩固。

HDLbits网站链接

https://hdlbits.01xz.net/wiki/Main_Page

想深耕嵌入式?这个专辑值得收藏

MCU、FPGA、工控、传感器一站式学习,实战项目直接抄

内容概要:本文针对海岛微电网中可再生能源消纳能力不足与运行经济性偏低的问题,提出了一种基于空调负荷与电动汽车(EV)联合虚拟储能的协同优化调度策略。通过挖掘温控负荷(如空调)的热惰性与电动汽车充电的时空灵活性,构建联合虚拟储能系统,有效增强微电网的储能调节能力。研究建立了融合源荷两侧资源的混合整数线性规划(MILP)优化模型,实现发电成本、购电成本与碳排放成本的多目标协同优化,并引入碳排放流理论量化系统各环节的碳排放强度,提升调度方案的低碳性。基于Matlab平台的仿真结果表明,所提策略在削峰填谷、降低系统综合运行成本、提高可再生能源利用率以及减少碳排放方面均具有显著效果,为孤立微电网的能量管理提供了可行的技术路径。; 适合人群:具备电力系统分析、优化调度或综合能源系统研究背景,熟悉Matlab编程与数学建模方法的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于海岛、偏远地区等孤立型微电网的能源优化管理;②探索高比例可再生能源接入下,需求侧灵活资源(如EV、温控负荷)参与电网调峰调频的协同控制机制;③实现微电网系统经济性、低碳性与运行稳定性的多维度优化目标; 阅读建议:建议结合提供的Matlab代码深入理解MILP优化模型的构建与求解过程,重点关注虚拟储能的建模方法、源荷协同调度框架的设计以及碳排放流理论在电力系统中的具体应用,适用于科研复现、论文写作及实际工程方案设计参考。
考虑阶梯碳交易-绿证联合机制的虚拟电厂多时间尺度优化调度(Matlab代码实现)内容概要:本文围绕“考虑阶梯碳交易-绿证联合机制的虚拟电厂多时间尺度优化调度”展开研究,提出了一种结合阶梯碳交易与绿色证书(绿证)机制的虚拟电厂优化调度模型,并通过Matlab代码实现仿真验证。研究构建了涵盖日前、日内和实时等多个时间尺度的调度框架,旨在提升虚拟电厂在低碳目标下的经济性与运行效率。模型综合考虑了多种分布式能源(如风电、光伏)、储能系统及可控负荷的协调运行,在满足电力供需平衡的同时,引入阶梯式碳交易成本函数和绿证收益机制,激励清洁能源消纳并降低碳排放。通过多时间尺度滚动优化,实现了系统在不同时间粒度上的精细化调度决策。; 适合人群:具备一定电力系统、能源管理或优化算法基础,从事新能源、虚拟电厂、碳交易等相关领域研究的科研人员及研究生。; 使用场景及目标:①应用于虚拟电厂参与电力市场与碳市场的协同优化调度;②为实现“双碳”目标下的多能源系统低碳经济运行提供决策支持;③服务于含高比例可再生能源接入的智能电网调度实践。; 阅读建议:建议读者结合文中提供的Matlab代码进行仿真实践,重点关注阶梯碳价设计、绿证激励机制与多时间尺度优化架构的耦合关系,宜配合YALMIP等优化工具包进行求解,以深入理解模型的构建逻辑与实际应用效果。
内容概要:本文系统阐述了基于去噪概率扩散模型(DDPM)的电动汽车充电行为场景生成方法,并配套提供了完整的Python代码实现。该方法通过深度学习中的扩散机制,对电动汽车用户的充电起始时间、持续时长、电量需求等关键行为特征进行建模,能够生成兼具统计合理性与高多样性的充电负荷场景,有效刻画实际充电行为的不确定性。所生成的场景可广泛应用于智能电网规划、配电网承载能力评估、需求响应策略制定、微电网优化调度等研究领域,为应对高比例电动汽车接入带来的挑战提供数据支撑。文档不仅聚焦算法实现,还强调科研思维的重要性,倡导研究者应逻辑严谨、善于借助成熟工具与资源,并鼓励创新性探索,避免盲目陷入技术细节。同时,文中整合了丰富的科研辅助资源,包括多个Matlab/Simulink仿真案例,覆盖电力系统优化、机器学习、综合能源管理等多个方向,形成了一个跨学科的技术支持体系。; 适合人群:具备一定Python编程基础,熟悉基本的数据分析与机器学习概念,从事电力系统、智能交通、综合能源系统、城市规划等领域的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①为电动汽车充电负荷的不确定性建模提供先进的深度学习解决方案;②生成高质量的输入场景以支撑配电网脆弱性分析、微电网经济调度、V2G(车辆到电网)潜力评估等仿真研究;③作为深度生成模型(如DDPM)在能源系统交叉领域应用的学习范例,推动AI for Science的研究实践。; 阅读建议:建议学习者以提供的Python代码为核心,结合网盘中的完整资源与公众号的技术支持,进行动手实践。应注重理解扩散模型的数学原理与代码实现之间的映射关系,通过调整模型参数和输入数据进行对比实验,深入掌握场景生成技术的关键环节,并将其灵活迁移至自身的研究课题中。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值