Simulation

package demo17

abstract class Simulation {

  type Action = () => Unit
case class WorkItem(time: Int, action: Action) private var curtime = 0 def currentTime: Int = curtime //日程,记录所有未执行的工作项目
private var agenda: List[WorkItem] = List() //更新排序
private def insert(ag: List[WorkItem], item: WorkItem): List[WorkItem] = {
//如果头部时间大于当前时间,那么直接头插
if(ag.isEmpty || item.time < ag.head.time) {
item :: ag
} else {
//否则遍历递归到合适位置
ag.head :: insert(ag.tail, item)
}
} //向日程添加工作项,第二个参数是传名参数
def afterDelay(delay: Int)(block: => Unit) = {
val item = WorkItem(currentTime + delay, () => block)
agenda = insert(agenda, item)
} private def next() = {
(agenda: @unchecked) match {
case item :: rest =>
agenda = rest
curtime = item.time
//执行这个工作项的操作
//这里一定要带括号,不然就是相当于get操作
item.action()
}
} def run() = {
//插入方法
afterDelay(0) {
println("*** simulation started, time = " + currentTime + " ***")
}
//执行所有的操作
while(!agenda.isEmpty) next()
} }

BasicCircuitSimulation

package demo17

abstract class BasicCircuitSimulation extends Simulation {

  //用来做门的延迟
def InverterDelay: Int
def AndGateDelay: Int
def OrGateDelay: Int //线
class Wire { private var sigVal = false
private var actions: List[Action] = List() def getSignal = sigVal
def setSignal(s: Boolean) = {
if(s != sigVal) {
sigVal = s
//这里是对actions每一个元素应用=》_ () 操作也就是f => f(),每次线的状态变化的时候,执行actions
actions foreach (_ ())
}
} def addAction(a: Action) = {
//加入载头部
actions = a :: actions
//并执行一次a
a()
}
} //非门,也就是翻转器,在uinput和output之间建立饭庄器
def inverter(input: Wire, output: Wire) = {
def invertAction() = {
val inputSig = input.getSignal
//在制定的延迟之后执行对应的方法
afterDelay(InverterDelay) {
//设置输出线信号为input相反
output setSignal !inputSig
}
//test
// output setSignal !inputSig
}
//添加动作到input线
input addAction invertAction
} //与门,输入两个线信号的&操作
def andGate(a1: Wire, a2: Wire, output: Wire) = {
def andAction() = {
val a1Sig = a1.getSignal
val a2Sig = a2.getSignal
afterDelay(AndGateDelay) {
output setSignal (a1Sig & a2Sig)
}
//test
// output setSignal (a1Sig & a2Sig)
}
a1 addAction andAction
a2 addAction andAction
} def orGate(o1: Wire, o2: Wire, output: Wire) = {
def orAction() = {
val o1Sig = o1.getSignal
val o2Sig = o2.getSignal
afterDelay(OrGateDelay) {
output setSignal (o1Sig | o2Sig)
}
//test
// output setSignal (o1Sig | o2Sig)
}
o1 addAction orAction
o2 addAction orAction
} //用来观察线的型号变化
def probe(name: String, wire: Wire) = {
def probeAction() = {
println(name + " " + currentTime + " new-value = " + wire.getSignal)
}
wire addAction probeAction
} }

CircuitSimulation

package demo17

abstract class CircuitSimulation extends BasicCircuitSimulation {

  def halfAdder(a: Wire, b: Wire, s: Wire, c: Wire) = {
val d, e = new Wire
orGate(a, b, d)
andGate(a, b, c)
inverter(c, e)
andGate(d, e, s)
} def fullAdder(a: Wire, b: Wire, cin: Wire, sum: Wire, cout: Wire) = {
val s, c1, c2 = new Wire
halfAdder(a, cin, s, c1)
halfAdder(b, s, sum, c2)
orGate(c1, c2, cout)
} }

MySimulation

package demo17

object MySimulation extends CircuitSimulation {
def InverterDelay: Int = 1 def AndGateDelay: Int = 3 def OrGateDelay: Int = 5 }

测试结果:

package demo17

import demo17.MySimulation._

object Demo18Test {
def main(args: Array[String]): Unit = {
val input1, input2, sum, carry = new Wire
probe("sum", sum)
probe("carry", carry)
halfAdder(input1, input2, sum, carry)
input1 setSignal true
run()
input2 setSignal true
run()
println("over")
}
}

测试结果:

模拟电路的半加器

【SCALA】3、模拟电路的更多相关文章

  1. 模拟电路"虚短" & "虚断"

    <虚短 & 虚断> 运算放大器组成的电路五花八门,令人眼花瞭乱,是模拟电路中学习的重点.遍观所有模拟电子技朮的书籍和课程,在介绍运算放大器电路的时候,无非是先给电路来个定性,比如这 ...

  2. 初级模拟电路:3-1 BJT概述

    回到目录 1.   名称由来 BJT的全称是双极性结型晶体管(Bipolar Junction Transistor),国内俗称三极管.其实,在英语中,三极管(triode)特指以前的真空电子管形式的 ...

  3. 初级模拟电路:4-1 BJT交流分析概述

    回到目录 BJT晶体管的交流分析(也叫小信号分析)是模拟电路中的一个难点,也可以说是模电中的一个分水岭.如果你能够把BJT交流分析的原理全都搞懂,那之后的学习就是一马平川了.后面的大部分内容,诸如:场 ...

  4. [模拟电路] 2、Passive Band Pass Filter

    note: Some articles are very good in http://www.electronics-tutorials.ws/,I share them in the Cnblog ...

  5. 初级模拟电路:1-2 PN结与二极管

    回到目录 1.   掺杂半导体 上面我们分析了本征半导体的导电情况,但由于本征半导体的导电能力很低,没什么太大用处.所以,一般我们会对本征半导体材料进行掺杂,即使只添加了千分之一的杂质,也足以改变半导 ...

  6. 初级模拟电路:4-3 BJT晶体管的交流建模

    回到目录 1. 四种BJT模型概述 对BJT晶体管建模的基本思路就是,用电路原理中的五大基本元件(电阻.电容.电感.电源.受控源)构建一个电路,使其在一定工作条件下能等效非线性半导体器件的实际工作.一 ...

  7. 初级模拟电路:3-11 BJT实现电流源

    回到目录 1. 恒流源 (1)简易恒流源 用BJT晶体管可以构造一个简易的恒流源,实现电路如下: 图3-11.01 前面我们在射极放大电路的分压偏置时讲过,分压偏置具有非常好的稳定性,几乎不受晶体管的 ...

  8. 初级模拟电路:3-2 BJT的工作原理

    回到目录 和前面介绍二极管的PN结的工作原理一样,BJT的量子级工作机制也非常复杂,一般教科书上为了帮助学习者能快速理解,也都是用一种简化模型的方法来介绍BJT的工作机理,一般只需大致了解即可.只要记 ...

  9. 初级模拟电路:3-8 BJT数据规格书(直流部分)

    回到目录 本小节我们以2N4123通用型BJT硅基晶体管为例,来介绍如何阅读BJT的数据规格书,点此链接可以阅读和下载2N4123的数据规格书. 1. 总体性能 打开datasheet后,首先看标题: ...

随机推荐

  1. Shell同步数据到oracle数据库

    某数据库有几张表更新,本地数据库增量更新数据,用脚本定时执行实现. 由于无blob/clob字段,使用sqlldr导入数据:查询目的数据库,以"|"分割导出所有数据,保存在./da ...

  2. html5中section元素详解

    html5中section元素详解 一.总结 一句话总结: section元素 用来定义文章中的章节(通常应该有标题和段落内容) section元素的作用就是给内容分段,给页面分区 1.section ...

  3. PHP如何解决网站大流量与高并发的问题(四)

    动态语言的并发处理 相关概念 什么是进程.线程.协程 什么是多进程.多线程 同步阻塞模型 异步非阻塞模型 php并发编程实践 什么是进程.线程.协程 进程 进程是一个执行中的程序 进程的三态模型:多道 ...

  4. Learning to rank基本算法

    搜索排序相关的方法,包括 Learning to rank 基本方法 Learning to rank 指标介绍 LambdaMART 模型原理 FTRL 模型原理 Learning to rank ...

  5. 降维算法整理--- PCA、KPCA、LDA、MDS、LLE 等

    转自github: https://github.com/heucoder/dimensionality_reduction_alo_codes 网上关于各种降维算法的资料参差不齐,同时大部分不提供源 ...

  6. Java性能分析神器-JProfiler详解(转)

    前段时间在给公司项目做性能分析,从简单的分析Log(GC log, postgrep log, hibernate statitistic),到通过AOP搜集软件运行数据,再到PET测试,感觉时间花了 ...

  7. 000 centos7下安装elasticsearch7的单节点安装

    在这里,直接使用最新的包进行学习.这里的安装也以前的版本不同,不过因为学习,这部分安装的区别不具体研究了. 这里也是摸索型的记录,所以会出现报错情况,然后针对这种方式进行解决,最后达到安装完成的效果. ...

  8. nxp基于layerscape系列芯片的硬件型号解析

    每一种layerscape系列芯片都有两种硬件型号: RDB 和QDS RDB: Refrence Design Board QDS: QorIQ Development system

  9. 解决 service iptables save 报错 please try to use systemctl

    本文档根据 service iptables save 报错 please try to use systemctl 提供解决方案.报错 [root@Jaking ~]# service iptabl ...

  10. Oracle中复制表的方法(create as select、insert into select、select into)

    转: Oracle中复制表的方法(create as select.insert into select.select into) 2018-07-30 22:10:37 小白白白又白cdllp 阅读 ...