module muxtwo(out a,b,sl);
input a,b,sl;
output reg out;
always@(sl or a or b)
if(!sl) out=a;
else out=b;
endmodule
解析:
第一行:module定义模块名称为muxtwo,括号内为输入,输出端名称a,b,sl,out)
第二行:定义输入端
第三行:定义输出端,这里的reg指寄存器类型,与之相对应的是wire类型,他们的区别是wire表示线通,即输入有变化,直接反应(如与、非门的简单连接),reg表示一定要有触发,输出才会反映输入的状态。wire一般用在组合逻辑中,reg一般用在时序逻辑中。
第四行:用于判断a,b,sl是否有一个变化,有就继续向下执行,always@(sl or a or b),即不带时钟边沿的,综合后是组合逻辑类型;相对应的always @(posedge clk)形式的,是边沿的,综合出来一般是时序逻辑,会包含触发器(Flip-Flop),
第五行与第六行构成if…else条件语句,当sl输入为低电平时,输出为a,反之为b。
第七行:endmodule指结构模块,同开始的module相对应。
示例一
前面我们已经知道了二选一的数据选择器的逻辑功能,因此可以画出真值表,和逻辑表达式:


module muxtwo(a,b,sl.out)
input a,b,sl;
output reg out;
wire nsl,sela,selb;
assign nsl=~sl;
assign sela=a&nsl;
assign selb=b&sl;
assign out=sela|selb;
endmodule
解析:
第一行:module定义模块名称为muxtwo,括号内为输入,输出端名称a,b,sl,out)
第二行:定义输入端。
第三行:定义输出端。
第四行:定义nsl,sela,selb线直接连接
第五行:assign nsl=~sl,表示是一个非门,后面的assign sela=a&nsl;
assign selb=b&sl;assign out=sela|selb;都是形容与门和或门这样简单的逻辑门连接关系,也就是为什么用wire和assign组合。> 第七行:endmodule指结构模块,同开始的module相对应。
数电中加法器分为全加器和半加器,此处有进位,是全加器
module adder(count,sum,a,b,cin)
input [2:0]a,b;
input cin;
output count;
output[2:0]sum;
assign[count,sum]=a+b+cin;
endmodule
第一行:定义加法器
第二行,第三行:定义输出三位数啊a,b和输出sum
第四行:定义输出端口
module comapre (a,b,equal)
output equal;
input [1:0]a,b;
assign equal=(a==b)? 1:0;
endmodule
关键在于第四行,利用assign 连续化语句进行判断等式子,实现1或者0的输出。
module trist1(sout,sin,ena);
output sout;
input sin,ena;
mytri tri_inst(.out(sout),in(sin),enable(ena));
ensmodule
module mytri(out,in,enable);
output out;
input in,enable;
assign out=enable?in:'bz;
ensmodule
解析:## 标题
本代码为双模块,其中注意‘bz指二进制高阻态,前面系数是0

initial
begin
areg=0;
for(index=0;index<size;index+1)
memory[index]=0;
end
reg tick;
reg[7:0] counter;
always@(posedge areg)
begin
counter=counter+1;
tick=~tick;
end
module traffic lights;
reg clock,red,amber,green;
parameter on=1,off=0,red_tics=350,amber_tics=30,green_tics=200;
initial red=off;
initial amber=off;
initial green=off;
always
begin
red=on;
light(red,red_tics);
green=on;
light(green,green_tics);
amber=on;
light(amber,amber_tics);
end
task light;
output color;
input[31:0]tics;
begin
repeat(tics)
@(posedge clock);
color=off;
end
endtask
always
begin
#100 clock=0;
#100 clock=1;m//#指代延时时间,时间长短有timescale决定
end
endmodule
例题一:

module out(a,b,c,d,f)
input a,b,c,d;
output f;
assign f=((a&b)&(c&d))
ensmodule
例题二:
第一种写法:
reg FF1,FF2,FF3;
always@(posedge(clock))
begin
FF1<=input;
FF2<=FF1;
FF3<=FF2;
end
第二种写法:
reg FF1,FF2,FF3;
always@(posedge clock)
output=FF3;
FF3=FF2;
FF2=FF1;
FF11=input;
end

例题三:加法器(全加器设计)
module add_4(X,Y,sum,C);
input[3:0]X,Y;
output[3:0]sum;
output C;
assign {C,Sum}=X+Y;
endmodule
例题四:乘法器
module mult_4(X,Y,mult)
input[3:0]X,Y;
output[7:0] Product;
assign Product=X*Y;
endmodule
例题五:比较器
module compare(X,Y,big,equal,small)
input [width-1:0] X,Y;
output small,big,equal;
reg samll,big,equal;]
parameter width=8;
always@(X or Y)
begin
if(X=Y)
equal=1;
else equal=0;
if(X>Y)
big=1;
else big=0;
if(X<Y)
small=1;
else small=0;
end
endmodule
module register8(ena,clk,data,rst,out);
input ena,clk,rst;
input[7:0] data;
output[7:0] out;
always@(posedge clk)
if(!rst)
out<=0;
else if(ena)
out<=data;
endmodule
module counter4(out,reset,clk)
out[3:0]out;
input reset,clk;
reg[3:0] out;
always@(posedge clk)
begin
if (reset) out<=0; //同步复位
else out<=out+1; //计数
end
endmodule
module decoder(out,in);
output[7:0] out;
input[2:0] in;
assign out =1'b1<<in;
ensmodule
module mux4(out,in0,in1,in2,in3,sel);
output out;
input in0,in1,in2,in3;
input [1:0]sel;
reg out;
always@(in0 or in1 or in2 or in3 or sel)
case(sel)
2'b00: out=in0;
2'b01: out=in1;
2'b10: out=in2;
2'b11: out=in3;
default: out=2'bx;
endcase
endmodule
timescale 1ns/1ns
module test;
reg a,b,c;
initial
begin
A=0;B=1;C=0;
#50 A=1;B=0;
#50 A=0;C=1;
#50 B=0;C=0;
#50 $finish
end
endmodule
module count60(qout,data,load,cin,reset,clk)
output[7:0] qout;
output cout;
input[7:0] data;
input load,cin,clk,reset;
reg[7:0] qout;
always @(posedge clk) //clk 上升沿时刻计
begin
if (reset) qout<=0; //同步复位
else if(load) qout<=data; //同步置数
else if(cin)
begin
if(qout[3:0]==9) //低位是否为 9,是则
begin
qout[3:0]<=0; //回 0,并判断高位是否为 5
if (qout[7:4]==5) qout[7:4]<=0;
else
qout[7:4]<=qout[7:4]+1; //高位不为 5,则加 1
end
else //低位不为 9,则加 1
qout[3:0]<=qout[3:0]+1;
end
end
assign cout=((qout==8'h59)&cin)?1:0;
endmodule
module funct(clk,n,result,reset);
output[31:0] result;
input[3:0] n;
input reset,clk;
reg[31:0] result;
always @(posedge clk) //在 clk 的上升沿时执行运算
begin
if(!reset) result<=0;
else begin
result <= 2 * factorial(n); //调用 factorial 函数
end
end
function[31:0] factorial; //阶乘运算函数定义(注意无端口列表)
input[3:0] opa; //函数只能定义输入端,输出端口为函数名本身
reg[3:0] i;
begin
factorial = opa ? 1 : 0;
for(i= 2; i <= opa; i = i+1)
factorial = i* factorial; //阶乘运算
end
endfunction
endmodule
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我有一个Controller,我想为这个Controller创建一个助手,我可以在不包含它的情况下使用它。我尝试像这样创建一个与Controller同名的助手classCars::EnginesController我创建的助手是moduleCars::EnginesHelperdefcheck_fuellogger.debug("chekingfuel")endend我得到的错误是undefinedlocalvariableormethod`check_fuel'for#有没有我遗漏的约定? 最佳答案 如果你真的想在Controll
我有一个模块stat存在于目录结构中:lib/stat_creator/stat/在lib/stat_creator/stat.rb中,我在lib/stat_creator/stat/目录中有我需要的文件,以及:moduleStatCreatormoduleStatendend当我使用该模块时,我将这些类称为StatCreator::Stat::Foo.new现在我想要一个存在于应用程序中的根Stat类。我在app/models中制作了我的Stat类,并在routes.rb中进行了设置。但是,如果我转到Rails控制台并尝试在应用程序/模型中使用Stat类,例如:Stat.by_use
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Testingmodulesinrspec目前我正在使用rspec成功测试我的模块,如下所示:require'spec_helper'moduleServicesmoduleAppServicedescribeAppServicedodescribe"authenticate"doit"shouldauthenticatetheuser"dopending"authenticatetheuser"endendendendend我的模块位于应用程序/服务/services.rb应用程序/服务/app_servi