使用FullAdder级联实现加法器
 
参考链接:
 
 
1.创建Add.java, 并生成构造方法和logic()方法
 
2. 根据逻辑原理图,添加输入输出线
 
 
3. 在构造方法中搜集输入输出线并调用construct()方法
 
4. 在logic()方法中创建子节点并连线
 
这里首先从input ports牵出线,并创建连接到output ports的线。其中,最后一个input port使用in(-1)取出。最后一个output port使用out(-1)取出。
 
cout作为一个游标,逐次指向每一级的进位线进行连接。最开始为cin,第一级之后代表这一级FullAdder的进位线...直到最后,代表最后一个进位线连接到Add节点的最后一个output port.
 
5. 创建inst静态方法方便后续使用
 
6. 创建main方法执行验证
 
 
运行结果为:
 
 
 
7. 生成Verilog
 
执行结果如下:
 
module Add_8后面多出来一个8?这样就可以把不同位宽的Add模块区分开来。
需要覆盖getName()方法:
 
原子节点需要覆盖primitive()方法,以返回原语的名称。比如与门,需要返回and:
 
 
更多实例请参考如下链接:
 
package org.jchdl.model.gsl.operator.arithmetic;
 
import org.jchdl.model.gsl.core.datatype.helper.WireVec;
import org.jchdl.model.gsl.core.datatype.net.Wire;
import org.jchdl.model.gsl.core.meta.Node;
import org.jchdl.model.gsl.core.value.Value;
 
// treat operands as plain bits
public class Add extends Node {
private int nBits = 0;
 
private WireVec in1;
private WireVec in2;
private Wire cin;
private WireVec sum;
private Wire cout;
 
public Add(WireVec sum, Wire cout, WireVec in1, WireVec in2, Wire cin) {
nBits = in1.nBits();
in(in1.wires());
in(in2.wires());
in(cin);
out(sum.wires());
out(cout);
construct();
}
 
@Override
public void logic() {
in1 = new WireVec(inputs(0, nBits));
in2 = new WireVec(inputs(nBits, 2*nBits));
cin = new Wire(in(-1));
 
sum = new WireVec(outputs(0, nBits));
 
cout = cin;
for (int i = 0; i < nBits; i++) {
Wire coutNext = new Wire();
FullAdder.inst(sum.wire(i), coutNext, in1.wire(i), in2.wire(i), cout);
cout = coutNext;
}
cout.connect(out(-1));
}
 
@Override
public String getName() {
return this.getClass().getSimpleName() + "_" + nBits;
}
 
public static Add inst(WireVec sum, Wire cout, WireVec in1, WireVec in2, Wire cin) {
return new Add(sum, cout, in1, in2, cin);
}
 
public static void main(String args[]) {
WireVec in1 = new WireVec(8);
WireVec in2 = new WireVec(8);
WireVec out = new WireVec(8);
Wire cin = new Wire();
Wire cout = new Wire();
 
Add.inst(out, cout, in1, in2, cin);
 
in1.assign(new Value[] { //0b0000_0010
Value.V0, Value.V1, Value.V0, Value.V0,
Value.V0, Value.V0, Value.V0, Value.V0,
});
in2.assign(new Value[] { // 0b1111_1111
Value.V1, Value.V1, Value.V1, Value.V1,
Value.V1, Value.V1, Value.V1, Value.V1,
});
cin.assign(Value.V1);
 
in1.propagate();
in2.propagate();
cin.propagate();
 
System.out.println("c_sum: " + cout + "_" + out);
 
Add.inst(out, cout, in1, in2, cin).toVerilog();
}
}
 
 
 

jchdl - GSL实例 - Add的更多相关文章

  1. jchdl - GSL实例 - Sub(二的补码实现)

    https://mp.weixin.qq.com/s/10fgjqPt2pRvIJzjDGYgBg   概念辨析   <IC-二进制, 自然数, 有符号数>:https://mp.weix ...

  2. jchdl - GSL实例 - ComplementTwo(二的补码)

    https://mp.weixin.qq.com/s/Gh2xJJvfg1SlyuayK4LRyQ   二的补码指对二进制数的所有位数整体求补.二进制运算下0,1互为补数,n位二进制数a的补数为2^n ...

  3. jchdl - GSL实例 - DFlipFlop(D触发器)

    https://mp.weixin.qq.com/s/7N3avTxTd2ZUnAcKg4w3Ig   D触发器对边沿敏感,只有当相应的边沿出现时,才会触发D的值传播到输出Q.   ​​ 引自:htt ...

  4. jchdl - GSL实例 - Div

    因为对除法研究不深,这里略去不表.   有兴趣可以参考链接: https://github.com/wjcdx/jchdl/blob/master/src/org/jchdl/model/gsl/op ...

  5. jchdl - GSL实例 - MulC2(有符号数的乘法)

      这里的实现,先把符号位取出来,使用两个正数相乘,然后在把符号加到乘积上.   参考链接 https://github.com/wjcdx/jchdl/blob/master/src/org/jch ...

  6. jchdl - GSL实例 - Mul(无符号数的乘法)

      这里实现最原始的阵列乘法,逐位相乘然后加到一起.   参考链接 https://github.com/wjcdx/jchdl/blob/edcc3e098d4f1cb21677e86e87a114 ...

  7. jchdl - GSL实例 - LogicalLeft

    https://mp.weixin.qq.com/s/WNm4bLWzZ0oWHWa7HQ6Y6w   逻辑左移,继承自Shifter类.只需要实现shift方法即可.   参考链接 https:// ...

  8. jchdl - GSL实例 - Shifter

    https://mp.weixin.qq.com/s/ngQji-xi4FCCbL_2ihUi_A   Shifter是移位节点的父类,定义了输入输出线,但是没有定义具体的移位方式,这个留给子类去实现 ...

  9. jchdl - GSL实例 - Concat

    https://mp.weixin.qq.com/s/oJY6Xj9_oM1gSmvH_dHkJg   Concat节点把多根输入线线组合成一排线输出.   参考链接 https://github.c ...

随机推荐

  1. C. Jury Marks 思维

    C. Jury Marks 这个题目虽然是只有1600,但是还是挺思维的. 有点难想. 应该可以比较快的推出的是这个肯定和前缀和有关, x x+a1 x+a1+a2 x+a1+a2+a3... x+s ...

  2. Codeforces Round #577 (Div. 2) D. Treasure Hunting

    Codeforces Round #577 (Div. 2)  D. Treasure Hunting 这个一场div2 前面三题特别简单,这个D题的dp还是比较难的,不过题目告诉你了只能往上走,所以 ...

  3. 201771010113 李婷华 《面向对象程序设计(Java)》第十一周总结

    一.理论知识部分 第九章  集合 1.数据结构介绍: 线性结构:线性表,栈,队列,串,数组,文件.非线性结构:树,图. 散列表:又称为哈希表. 散列表算法的基本思想是:以结点的关键字为自变量,通过一定 ...

  4. LeetCode--Array--Container With Most Water (Medium)

    11. Container With Most Water (Medium)# Given n non-negative integers a1, a2, ..., an , where each r ...

  5. 【Java基础总结】Java基础语法篇(上)

    Java基础语法 思维导图 一.Java语言介绍 1.Java应用平台 JavaSE(Java Platform Standard Edition):开发普通桌面和商务应用程序,是另外两类的基础 Ja ...

  6. ABAP 内表与XML转换

    1需求说明 在系统交互中需要将SAP内表转换为XML文件,发送给其他系统,并且将其他系统返回的XML文件转换为SAP内表. 2创建转换编辑器 事务代码:STRANS 选择简单转换 以图形方式编辑 右键 ...

  7. STM32 ADC多通道规则采样和注入采样

    layout: post tags: [STM32] comments: true 文章目录 layout: post tags: [STM32] comments: true 什么是ADC? STM ...

  8. Vue + Element-ui实现后台管理系统(5)---封装一个Form表单组件和Table表格组件

    封装一个Form表单组件和Table组件 有关后台管理系统之前写过四遍博客,看这篇之前最好先看下这四篇博客.另外这里只展示关键部分代码,项目代码放在github上: mall-manage-syste ...

  9. md5函数

    0x01 <?php error_reporting(0); $flag = 'flag{test}'; if (isset($_GET['username']) and isset($_GET ...

  10. java ->String、StringBuffer、StringBuilder三者之间的区别

    1.首先说运行速度,速度由快到慢排列:StringBuilder > StringBuffer > String String最慢的原因: String为字符串常量,而StringBuil ...