经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
JS模拟实现串行加法器
来源:jb51  时间:2022/3/14 10:01:49  对本文有异议

在重温《编码:隐匿在计算机软硬件背后的语言》第12章——二进制加法器时,心血来潮用JS写了一个模拟串行加法器。

测试断言工具TestUtils.js

  1. function assertTrue(actual){
  2. ? ? if(!actual)
  3. ? ? ? ? throw "Error actual: " + actual + " is not true."
  4. }
  5.  
  6. function assertFalse(actual){
  7. ? ? if(actual)
  8. ? ? ? ? throw "Error actual: " + actual + " is not false."
  9. }
  10.  
  11. function assertIntEquals(expected, actual){
  12. ? ? if(typeof expected != "number")
  13. ? ? ? ? throw "Error expected: " + expected +" is not a number."
  14.  
  15. ? ? if(typeof actual != "number")
  16. ? ? ? ? throw "Error actual: " + actual +" is not a number."
  17.  
  18. ? ? if(expected != actual)
  19. ? ? ? ? throw "Error expected: " + expected + " and actual: " + actual +" is different."
  20. }

十进制数与二进制数之间转换工具utils.js

  1. function int2LowEightBitArray(num){
  2. ? ? var result = []
  3. ? ? for(var i = 0; i < 8; i++)
  4. ? ? ? ? result.push(((num >> i) & 1) == 1)
  5. ? ? return result
  6. }
  7.  
  8. function lowEightBitArray2Int(array){
  9. ? ? var result = 0
  10. ? ? for(var i = 0; i < 8; i++){
  11. ? ? ? ? if(array[i])
  12. ? ? ? ? ? ? result += (1 << i)
  13. ? ? }
  14. ? ? return result
  15. }
  16.  
  17. function lowNineBitArray2Int(array){
  18. ? ? var result = 0
  19. ? ? for(var i = 0; i < 9; i++){
  20. ? ? ? ? if(array[i])
  21. ? ? ? ? ? ? result += (1 << i)
  22. ? ? }
  23. ? ? return result
  24. }
  25.  
  26. //用补码表示负数
  27. function int2EightBitArray(num){
  28. ? ? if(num < -128 || num > 127){
  29. ? ? ? ? throw "Out of boundary(-128, 127)."
  30. ? ? }
  31. ? ? var result = []
  32. ? ? for(var i = 0; i < 8; i++)
  33. ? ? ? ? result.push(((num >> i) & 1) == 1)
  34. ? ? return result
  35. }
  36.  
  37. function eightBitArray2Int(array){
  38. ? ? var result = 0
  39. ? ? for(var i = 0; i < 7; i++){
  40. ? ? ? ? if(array[i])
  41. ? ? ? ? ? ? result += (1 << i)
  42. ? ? }
  43. ? ? if(array[i])
  44. ? ? ? ? result += -128
  45. ? ? return result
  46. }
  47.  
  48. function int2SixteenBitArray(num){
  49. ? ? if(num < -(1 << 15) || num > (1 << 15) - 1){
  50. ? ? ? ? throw "Out of boundary({left}, {right})."
  51. ? ? ? ? ? ? .replace("{left}", -(1 << 15))
  52. ? ? ? ? ? ? .replace("{right}", (1 << 15) - 1)
  53. ? ? }
  54. ? ? var result = []
  55. ? ? for(var i = 0; i < 16; i++)
  56. ? ? ? ? result.push(((num >> i) & 1) == 1)
  57. ? ? return result
  58. }
  59.  
  60. function sixteentBitArray2Int(array){
  61. ? ? var result = 0
  62. ? ? for(var i = 0; i < 15; i++){
  63. ? ? ? ? if(array[i])
  64. ? ? ? ? ? ? result += (1 << i)
  65. ? ? }
  66. ? ? if(array[i])
  67. ? ? ? ? result += -(1 << 15)
  68. ? ? return result
  69. }

加法器模型model.js

  1. class DoubleInGate{
  2. ? ? #id
  3. ? ? #in1
  4. ? ? #in2
  5. ? ? #gateTypeName
  6. ? ? #func
  7. ? ? #out
  8. ? ? #lastOut
  9. ? ? #outReceiver
  10. ? ? #outReceiverInName
  11.  
  12. ? ? constructor(id, gateTypeName, in1 = false, in2 = false, func){
  13. ? ? ? ? this.#id = id
  14. ? ? ? ? this.#gateTypeName = gateTypeName
  15. ? ? ? ? this.#in1 = in1
  16. ? ? ? ? this.#in2 = in2
  17. ? ? ? ? this.#func = func
  18. ? ? ? ? this.#out = this.#func(this.in1, this.in2)
  19. ? ? ? ? // this.#lastOut = this.#out
  20. ? ? }
  21. ? ??
  22. ? ? updateIn1(flag){
  23. ? ? ? ? this.#in1 = flag
  24. ? ? ? ? this.#updateOut()
  25. ? ? }
  26. ? ??
  27. ? ? updateIn2(flag){
  28. ? ? ? ? this.#in2 = flag
  29. ? ? ? ? this.#updateOut()
  30. ? ? }
  31. ? ??
  32. ? ? getOut(){
  33. ? ? ? ? return this.#out;
  34. ? ? }
  35.  
  36. ? ? updateBothIn(in1 = false, in2 = false){
  37. ? ? ? ? this.#in1 = in1
  38. ? ? ? ? this.#in2 = in2
  39. ? ? ? ? this.#updateOut()
  40. ? ? }
  41. ? ??
  42. ? ? setOutReceiver(outReceiver, inName){
  43. ? ? ? ? this.#outReceiver = outReceiver
  44. ? ? ? ? this.#outReceiverInName = inName
  45. ? ? }
  46.  
  47. ? ? #updateOut(){
  48. ? ? ? ? this.#lastOut = this.#out
  49. ? ? ? ? this.#out = this.#func(this.#in1, this.#in2)
  50. ? ? ? ? if(this.#out != this.#lastOut){
  51. ? ? ? ? ? ? this.#send(this.#out)
  52. ? ? ? ? }
  53. ? ? }
  54.  
  55. ? ? #send(flag){
  56. ? ? ? ? if(this.#outReceiver){
  57. ? ? ? ? ? ? this.#outReceiver.receive(this.#outReceiverInName, flag)
  58. ? ? ? ? }
  59. ? ? }
  60.  
  61. ? ? receive(inName, flag){
  62. ? ? ? ? if(inName == "in1"){
  63. ? ? ? ? ? ? this.updateIn1(flag)
  64. ? ? ? ? }else if(inName == "in2"){
  65. ? ? ? ? ? ? this.updateIn2(flag)
  66. ? ? ? ? }
  67. ? ? }
  68.  
  69. ? ? toString(){
  70. ? ? ? ? return "{gateTypeName} id: {id}, in1: {in1}, in2: {in2}, out:{out}"
  71. ? ? ? ? ? ? .replace("{gateTypeName}", this.#gateTypeName)
  72. ? ? ? ? ? ? .replace("{id}", this.#id)
  73. ? ? ? ? ? ? .replace("{in1}", this.#in1)
  74. ? ? ? ? ? ? .replace("{in2}", this.#in2)
  75. ? ? ? ? ? ? .replace("{out}", this.#out)
  76. ? ? }
  77. }
  78.  
  79. //与门
  80. class AndGate extends DoubleInGate{
  81. ? ? constructor(id, in1 = false, in2 = false){
  82. ? ? ? ? super(id, "AndGate", in1, in2, (i1, i2) => i1 && i2)
  83. ? ? }
  84. }
  85.  
  86. //或门
  87. class OrGate extends DoubleInGate{
  88. ? ? constructor(id, in1 = false, in2 = false){
  89. ? ? ? ? super(id, "OrGate", in1, in2, (i1, i2) => i1 || i2)
  90. ? ? }
  91. }
  92.  
  93. //异或门
  94. class XorGate extends DoubleInGate{
  95. ? ? constructor(id, in1 = false, in2 = false){
  96. ? ? ? ? //^在js按位异或, 而在java中不是
  97. ? ? ? ? super(id, "XorGate", in1, in2, (i1, i2) => (i1 || i2) && !(i1 && i2))?
  98. ? ? }
  99. }
  100.  
  101. class SingleInGate{
  102. ? ? #id
  103. ? ? #in
  104. ? ? #out
  105. ? ? #lastOut
  106. ? ? #func
  107. ? ? #outReceiver
  108. ? ? #outReceiverInName
  109.  
  110. ? ? constructor(id, in_, func){
  111. ? ? ? ? this.#id = id
  112. ? ? ? ? this.#in = in_
  113. ? ? ? ? this.#func = func
  114. ? ? ? ? this.#out = this.#func(this.#in)
  115. ? ? ? ? // this.#lastOut = this.#out
  116. ? ? }
  117.  
  118. ? ? updateIn(flag){
  119. ? ? ? ? this.#in = flag
  120. ? ? ? ? this.#updateOut()
  121. ? ? }
  122.  
  123. ? ? getOut(){
  124. ? ? ? ? return this.#out
  125. ? ? }
  126.  
  127. ? ? //注册输出端口接收者,(类观察者模式)
  128. ? ? setOutReceiver(outReceiver, inName){
  129. ? ? ? ? this.#outReceiver = outReceiver
  130. ? ? ? ? this.#outReceiverInName = inName
  131. ? ? }
  132.  
  133. ? ? #updateOut(){
  134. ? ? ? ? this.#lastOut = this.#out
  135. ? ? ? ? this.#out = this.#func(this.#in)
  136. ? ? ? ? if(this.#out != this.#lastOut){
  137. ? ? ? ? ? ? this.#send(this.#out)
  138. ? ? ? ? }
  139. ? ? }
  140.  
  141. ? ? #send(flag){
  142. ? ? ? ? if(this.#outReceiver){
  143. ? ? ? ? ? ? this.#outReceiver.receive(this.#outReceiverInName, flag)
  144. ? ? ? ? } ? ?
  145. ? ? }
  146.  
  147. ? ? receive(inName, flag){
  148. ? ? ? ? if(inName == "in"){ //TODO 或许有更灵活的写法
  149. ? ? ? ? ? ? this.updateIn(flag)
  150. ? ? ? ? }
  151. ? ? }
  152. }
  153.  
  154. class NullGate extends SingleInGate{
  155. ? ? constructor(id, in_= false){
  156. ? ? ? ? super(id, in_, a=>a)
  157. ? ? }
  158. }
  159.  
  160. class NotGate extends SingleInGate{
  161. ? ? constructor(id, in_= false){
  162. ? ? ? ? super(id, in_, a=>!a)
  163. ? ? }
  164. }
  165.  
  166. class Adder{
  167. ? ? #id
  168. ? ? #inA
  169. ? ? #inB
  170. ? ??
  171. ? ? #outS
  172. ? ? #outC
  173. ? ? #lastOutS
  174. ? ? #lastOutC
  175.  
  176. ? ? #outSReceiver
  177. ? ? #outSReceiverInName
  178. ? ? #outCReceiver
  179. ? ? #outCReceiverInName
  180.  
  181. ? ? constructor(id, inA=false, inB=false, outS, outC){
  182. ? ? ? ? this.#id = id
  183. ? ? ? ? this.#inA = inA
  184. ? ? ? ? this.#inB = inB
  185. ? ? ? ? this.#outS = outS
  186. ? ? ? ? this.#outC = outC
  187. ? ? }
  188.  
  189. ? ? updateInA(flag, func){
  190. ? ? ? ? this.#inA = flag
  191. ? ? ? ? this.updateOutSAndOutC(func)
  192. ? ? }
  193.  
  194. ? ? updateInB(flag, func){
  195. ? ? ? ? this.#inB = flag
  196. ? ? ? ? this.updateOutSAndOutC(func)
  197. ? ? }
  198.  
  199. ? ? updateBothIn(inA, inB, func){
  200. ? ? ? ? this.#inA = inA
  201. ? ? ? ? this.#inB = inB
  202. ? ? ? ? this.updateOutSAndOutC(func)
  203. ? ? }
  204.  
  205. ? ? getOutS(){
  206. ? ? ? ? return this.#outS
  207. ? ? }
  208.  
  209. ? ? getOutC(){
  210. ? ? ? ? return this.#outC
  211. ? ? }
  212.  
  213. ? ? #updateOutS(flag){
  214. ? ? ? ? this.#lastOutS = this.#outS
  215. ? ? ? ? this.#outS = flag
  216. ? ? ? ? if(this.#outS != this.#lastOutS){
  217. ? ? ? ? ? ? this.#sendOutS(this.#outS)
  218. ? ? ? ? }
  219. ? ? }
  220. ? ??
  221. ? ? #updateOutC(flag){
  222. ? ? ? ? this.#lastOutC = this.#outC
  223. ? ? ? ? this.#outC = flag
  224. ? ? ? ? if(this.#outC != this.#lastOutC){
  225. ? ? ? ? ? ? this.#sendOutC(this.#outC)
  226. ? ? ? ? }
  227. ? ? }
  228. ? ??
  229. ? ? updateOutSAndOutC(func){
  230. ? ? ? ? if(func){
  231. ? ? ? ? ? ? var results = func()
  232. ? ? ? ? ? ? this.#updateOutS(results[0])
  233. ? ? ? ? ? ? this.#updateOutC(results[1])
  234. ? ? ? ? }
  235. ? ? }
  236.  
  237. ? ? setOutSReceiver(outSReceiver, inName){
  238. ? ? ? ? this.#outSReceiver = outSReceiver
  239. ? ? ? ? this.#outSReceiverInName = inName
  240. ? ? }
  241.  
  242. ? ? setOutCReceiver(outCReceiver, inName){
  243. ? ? ? ? this.#outCReceiver = outCReceiver
  244. ? ? ? ? this.#outCReceiverInName = inName
  245. ? ? }
  246.  
  247. ? ? receive(inName, flag){
  248. ? ? ? ? if(inName == "inA"){
  249. ? ? ? ? ? ? this.updateInA(flag)
  250. ? ? ? ? }else if(inName == "inB"){
  251. ? ? ? ? ? ? this.updateInB(flag)
  252. ? ? ? ? }
  253. ? ? }
  254.  
  255. ? ? #sendOutS(flag){
  256. ? ? ? ? if(this.#outSReceiver){
  257. ? ? ? ? ? ? this.#outSReceiver.receive(this.#outSReceiverInName, flag)
  258. ? ? ? ? }
  259. ? ? }
  260.  
  261. ? ? #sendOutC(flag){
  262. ? ? ? ? if(this.#outCReceiver){
  263. ? ? ? ? ? ? this.#outCReceiver.receive(this.#outCReceiverInName, flag)
  264. ? ? ? ? }
  265. ? ? }
  266. }
  267.  
  268. class HalfAdder extends Adder{
  269. ? ? #xorGate
  270. ? ? #andGate
  271.  
  272. ? ? constructor(id, inA=false, inB=false){
  273. ? ? ? ? var xorGate = new XorGate(undefined, inA, inB);
  274. ? ? ? ? var andGate = new AndGate(undefined, inA, inB);
  275. ? ? ? ? super(id, inA, inB, xorGate.getOut(), andGate.getOut())
  276. ? ? ? ? this.#xorGate = xorGate
  277. ? ? ? ? this.#andGate = andGate?
  278. ? ? }
  279.  
  280. ? ? #returnOutArray(){
  281. ? ? ? ? return [this.#xorGate.getOut(), this.#andGate.getOut()]
  282. ? ? }
  283.  
  284. ? ? updateInA(flag){
  285. ? ? ? ? super.updateInA(flag, ()=>{
  286. ? ? ? ? ? ? this.#xorGate.updateIn1(flag)
  287. ? ? ? ? ? ? this.#andGate.updateIn1(flag)
  288. ? ? ? ? ? ? return this.#returnOutArray()
  289. ? ? ? ? })
  290. ? ? }
  291.  
  292. ? ? updateInB(flag){
  293. ? ? ? ? super.updateInB(flag, ()=>{
  294. ? ? ? ? ? ? this.#xorGate.updateIn2(flag)
  295. ? ? ? ? ? ? this.#andGate.updateIn2(flag)
  296. ? ? ? ? ? ? return this.#returnOutArray()
  297. ? ? ? ? })
  298. ? ? }
  299.  
  300. ? ? updateBothIn(inA, inB){
  301. ? ? ? ? super.updateBothIn(inA, inB, ()=>{
  302. ? ? ? ? ? ? this.#xorGate.updateBothIn(inA, inB)
  303. ? ? ? ? ? ? this.#andGate.updateBothIn(inA, inB)
  304. ? ? ? ? ? ? return this.#returnOutArray()
  305. ? ? ? ? })
  306. ? ? }
  307.  
  308. }
  309.  
  310. class FullAdder extends Adder{
  311. ? ? #inC
  312. ? ? #halfAdder1
  313. ? ? #halfAdder2
  314. ? ? #orGate
  315.  
  316. ? ? constructor(id, inA = false, inB = false, inC = false){
  317. ? ? ? ? var halfAdder1 = new HalfAdder(undefined, inA, inB)
  318. ? ? ? ? var halfAdder2 = new HalfAdder(undefined, inC, halfAdder1.getOutS())
  319. ? ? ? ? var orGate = new OrGate(undefined, halfAdder1.getOutC(), halfAdder2.getOutC())
  320. ? ? ? ? super(id, inA, inB, halfAdder2.getOutS(), orGate.getOut())
  321. ? ? ? ? this.#inC = inC
  322. ? ? ? ? this.#halfAdder1 = halfAdder1
  323. ? ? ? ? this.#halfAdder2 = halfAdder2
  324. ? ? ? ? this.#orGate = orGate
  325.  
  326. ? ? ? ? this.#halfAdder1.setOutSReceiver(halfAdder2, "inB")
  327. ? ? ? ? this.#halfAdder1.setOutCReceiver(orGate, "in1")
  328. ? ? ? ? this.#halfAdder2.setOutCReceiver(orGate, "in2")
  329. ? ? }
  330.  
  331. ? ? #returnOutArray(){
  332. ? ? ? ? return [this.#halfAdder2.getOutS(), this.#orGate.getOut()]
  333. ? ? }
  334.  
  335. ? ? updateInA(flag){
  336. ? ? ? ? super.updateInA(flag, ()=>{
  337. ? ? ? ? ? ? this.#halfAdder1.updateInA(flag)
  338. ? ? ? ? ? ? return this.#returnOutArray()
  339. ? ? ? ? })
  340. ? ? }
  341.  
  342. ? ? updateInB(flag){
  343. ? ? ? ? super.updateInB(flag, ()=>{
  344. ? ? ? ? ? ? this.#halfAdder1.updateInB(flag)
  345. ? ? ? ? ? ? return this.#returnOutArray()
  346. ? ? ? ? })
  347. ? ? }
  348.  
  349. ? ? updateInC(flag){
  350. ? ? ? ? this.#inC = flag
  351. ? ? ? ? this.#halfAdder2.updateInA(flag)
  352. ? ? ? ? this.updateOutSAndOutC(()=>{
  353. ? ? ? ? ? ? return this.#returnOutArray()
  354. ? ? ? ? })
  355. ? ? }
  356.  
  357. ? ? updateBothIn(inA, inB){
  358. ? ? ? ? super.updateBothIn(inA, inB, ()=>{
  359. ? ? ? ? ? ? this.#halfAdder1.updateBothIn(inA, inB)
  360. ? ? ? ? ? ? return this.#returnOutArray()
  361. ? ? ? ? })
  362. ? ? }
  363.  
  364. ? ? updateThreeIn(inA, inB, inC){
  365. ? ? ? ? super.updateBothIn(inA, inB)
  366. ? ? ? ? this.#inC = inC
  367.  
  368. ? ? ? ? this.#halfAdder1.updateBothIn(inA, inB)
  369. ? ? ? ? this.#halfAdder2.updateBothIn(inC, this.#halfAdder1.getOutS())
  370. ? ? ? ? this.#orGate.updateBothIn(this.#halfAdder1.getOutC(), this.#halfAdder2.getOutC())
  371.  
  372. ? ? ? ? this.updateOutSAndOutC(()=>{
  373. ? ? ? ? ? ? return this.#returnOutArray()
  374. ? ? ? ? })
  375. ? ? }
  376.  
  377. ? ? receive(inName, flag){
  378. ? ? ? ? super.receive(inName, flag)
  379. ? ? ? ? if(inName == "inC"){
  380. ? ? ? ? ? ? this.updateInC(flag)
  381. ? ? ? ? }
  382. ? ? }
  383. }
  384.  
  385. class EightBitBinaryAdder{
  386. ? ? #inC
  387. ? ? #outC
  388. ? ? #lastOutC
  389.  
  390. ? ? #inA
  391. ? ? #inB
  392. ? ? #outS
  393.  
  394. ? ? #fullAdders
  395. ? ? #fullAdderNum
  396.  
  397. ? ? #outCReceiver
  398. ? ? #outCReceiverInName
  399.  
  400. ? ? #outCInOutSFlag
  401.  
  402. ? ? constructor(outCInOutSFlag = false){
  403. ? ? ? ? this.#outCInOutSFlag = outCInOutSFlag
  404. ? ? ? ? this.#inC = false
  405. ? ? ? ? this.#fullAdderNum = 8
  406. ? ? ? ? this.#fullAdders = []
  407.  
  408. ? ? ? ? this.#inA = []
  409. ? ? ? ? this.#inB = []
  410.  
  411. ? ? ? ? for(var i = 0; i < this.#fullAdderNum; i++){
  412. ? ? ? ? ? ? this.#inA.push(false)
  413. ? ? ? ? ? ? this.#inB.push(false)
  414. ? ? ? ? }
  415.  
  416. ? ? ? ? //新键8个全加器
  417. ? ? ? ? for(var i = 0; i < this.#fullAdderNum; i++){
  418. ? ? ? ? ? ? this.#fullAdders.push(new FullAdder("f" + i))
  419. ? ? ? ? ? ? if(i != 0){
  420. ? ? ? ? ? ? ? ? this.#fullAdders[i - 1].setOutCReceiver(this.#fullAdders[i], "inC")
  421. ? ? ? ? ? ? }
  422. ? ? ? ? }
  423.  
  424. ? ? ? ? this.updateOut()
  425. ? ? }
  426.  
  427. ? ? updateBothIn(arrayA, arrayB){
  428. ? ? ? ? // this.#inC = inC
  429. ? ? ? ? this.#inA = arrayA
  430. ? ? ? ? this.#inB = arrayB
  431.  
  432. ? ? ? ? for(var i = 0; i < this.#fullAdderNum; i++){
  433. ? ? ? ? ? ? if(i == 0){
  434. ? ? ? ? ? ? ? ? this.#fullAdders[i].updateInC(this.#inC)
  435. ? ? ? ? ? ? }
  436. ? ? ? ? ? ? this.#fullAdders[i].updateBothIn(arrayA[i], arrayB[i])
  437. ? ? ? ? }
  438. ? ? ? ? this.updateOut()
  439. ? ? }
  440.  
  441. ? ? //输出默认值
  442. ? ? updateOut(){
  443. ? ? ? ? this.#outS = []
  444.  
  445. ? ? ? ? for(var i = 0; i < this.#fullAdderNum; i++){
  446. ? ? ? ? ? ? this.#outS.push(this.#fullAdders[i].getOutS())
  447. ? ? ? ? ? ? if(i == this.#fullAdderNum - 1){
  448. ? ? ? ? ? ? ? ? this.#lastOutC = this.#outC
  449. ? ? ? ? ? ? ? ? this.#outC = this.#fullAdders[i].getOutC()
  450.  
  451. ? ? ? ? ? ? ? ? if(this.#lastOutC != this.#outC)
  452. ? ? ? ? ? ? ? ? ? ? this.#sendOutC(this.#outC)
  453.  
  454. ? ? ? ? ? ? ? ? if(this.#outCInOutSFlag)
  455. ? ? ? ? ? ? ? ? ? ? this.#outS.push(this.#outC)
  456.  
  457. ? ? ? ? ? ? }
  458. ? ? ? ? }
  459. ? ? }
  460.  
  461. ? ? updateInC(flag){
  462. ? ? ? ? this.#inC = flag
  463. ? ? ? ? this.updateBothIn(this.#inA, this.#inB, flag)
  464. ? ? }
  465.  
  466. ? ? receive(inName, flag){
  467. ? ? ? ? if(inName == "inC"){
  468. ? ? ? ? ? ? this.updateInC(flag)
  469. ? ? ? ? }
  470. ? ? }
  471.  
  472. ? ? #sendOutC(flag){
  473. ? ? ? ? if(this.#outCReceiver){
  474. ? ? ? ? ? ? this.#outCReceiver.receive(this.#outCReceiverInName, flag)
  475. ? ? ? ? }
  476. ? ? }
  477.  
  478. ? ? setOutCReceiver(outCReceiver, inName){
  479. ? ? ? ? this.#outCReceiver = outCReceiver
  480. ? ? ? ? this.#outCReceiverInName = inName
  481. ? ? }
  482.  
  483. ? ? getOut(){
  484. ? ? ? ? return this.#outS
  485. ? ? }
  486. }
  487.  
  488. class SixteenBitBinaryAdder{
  489. ? ? #inC
  490. ? ? #outC
  491. ? ? #lastOutC
  492.  
  493. ? ? #inA
  494. ? ? #inB
  495. ? ? #outS
  496.  
  497. ? ? #eightBitBinaryAdder1
  498. ? ? #eightBitBinaryAdder2
  499.  
  500. ? ? #bitNum = 16
  501.  
  502. ? ? #outCReceiver
  503. ? ? #outCReceiverInName
  504.  
  505. ? ? #outCInOutSFlag
  506.  
  507. ? ? constructor(outCInOutSFlag){
  508. ? ? ? ? this.#outCInOutSFlag = outCInOutSFlag
  509. ? ? ? ? this.#inC = false
  510. ? ? ? ? this.#eightBitBinaryAdder1 = new EightBitBinaryAdder()
  511. ? ? ? ? this.#eightBitBinaryAdder2 = new EightBitBinaryAdder()
  512.  
  513. ? ? ? ? this.#inA = []
  514. ? ? ? ? this.#inB = []
  515.  
  516. ? ? ? ? for(var i = 0; i < this.#bitNum; i++){
  517. ? ? ? ? ? ? this.#inA.push(false)
  518. ? ? ? ? ? ? this.#inB.push(false)
  519. ? ? ? ? }
  520.  
  521. ? ? ? ? this.#eightBitBinaryAdder1.setOutCReceiver(this.#eightBitBinaryAdder2, "inC")
  522.  
  523. ? ? ? ? this.updateOut()
  524. ? ? }
  525.  
  526. ? ? updateBothIn(arrayA, arrayB){
  527. ? ? ? ? this.#inA = arrayA
  528. ? ? ? ? this.#inB = arrayB
  529. ? ? ? ? this.#eightBitBinaryAdder1.updateBothIn(arrayA.slice(0, 8), arrayB.slice(0, 8))
  530. ? ? ? ? this.#eightBitBinaryAdder2.updateBothIn(arrayA.slice(8), arrayB.slice(8))
  531. ? ? ? ? this.updateOut()
  532. ? ? }
  533.  
  534. ? ? updateOut(){
  535. ? ? ? ? this.#outS = this.#eightBitBinaryAdder1.getOut().concat(this.#eightBitBinaryAdder2.getOut())
  536. ? ? ? ? //发送send(OutC)
  537. ? ? }
  538. ? ? getOut(){
  539. ? ? ? ? return this.#outS
  540. ? ? }
  541.  
  542. ? ? //TODO
  543. ? ? // updateInC(flag){
  544. ? ? // ? ? this.#inC = flag
  545.  
  546. ? ? // ? ? this.updateBothIn(this.#inA, this.#inB, flag)
  547. ? ? // }
  548.  
  549. ? ? // receive(inName, flag){
  550. ? ? // ? ? if(inName == "inC"){
  551. ? ? // ? ? ? ? this.updateInC(flag)
  552. ? ? // ? ? }
  553. ? ? // }
  554.  
  555. ? ? #sendOutC(flag){
  556. ? ? ? ? if(this.#outCReceiver){
  557. ? ? ? ? ? ? this.#outCReceiver.receive(this.#outCReceiverInName, flag)
  558. ? ? ? ? }
  559. ? ? }
  560.  
  561. ? ? setOutCReceiver(outCReceiver, inName){
  562. ? ? ? ? this.#outCReceiver = outCReceiver
  563. ? ? ? ? this.#outCReceiverInName = inName
  564. ? ? }
  565.  
  566. }

运行验证代码testModel.js
验证通过判据:后台没有打印输出异常。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. ? ? <meta charset="UTF-8">
  5. ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. ? ? <title>Test</title>
  8. ? ? <script src="../js/test/TestUtils.js"></script>
  9. ? ? <script src="../js/model.js"></script>
  10. ? ? <script src="../js/utils.js"></script>
  11. </head>
  12. <body>
  13.  
  14. ? ? <script>
  15. ? ? ? ? var g1 = new NullGate("g1")
  16. ? ? ? ? var g2 = new NullGate("g2")
  17. ? ? ? ? var g3 = new NullGate("g3")
  18. ? ? ? ? var g4 = new NullGate("g4")
  19. ? ? ? ? var g5 = new NullGate("g5")
  20. ? ? ? ? var g6 = new NullGate("g6")
  21.  
  22. ? ? ? ? g1.setOutReceiver(g2, "in")
  23. ? ? ? ? g2.setOutReceiver(g3, "in")
  24. ? ? ? ? g3.setOutReceiver(g4, "in")
  25. ? ? ? ? g4.setOutReceiver(g5, "in")
  26. ? ? ? ? g5.setOutReceiver(g6, "in")
  27.  
  28. ? ? ? ? g1.updateIn(true)
  29. ? ? ? ? assertTrue(g6.getOut())
  30. ? ? </script>
  31.  
  32. ? ? <script>
  33. ? ? ? ? //与门
  34. ? ? ? ? var andGate = new AndGate("a01")
  35. ? ? ? ? assertFalse(andGate.getOut())
  36.  
  37. ? ? ? ? andGate.updateIn1(true)
  38. ? ? ? ? assertFalse(andGate.getOut())
  39.  
  40. ? ? ? ? andGate.updateIn2(true)
  41. ? ? ? ? assertTrue(andGate.getOut())
  42.  
  43. ? ? ? ? //或门
  44. ? ? ? ? var orGate = new OrGate("o01")
  45. ? ? ? ? assertFalse(orGate.getOut())
  46.  
  47. ? ? ? ? orGate.updateIn1(true)
  48. ? ? ? ? assertTrue(orGate.getOut())
  49.  
  50. ? ? ? ? orGate.updateIn2(true)
  51. ? ? ? ? assertTrue(orGate.getOut())
  52.  
  53. ? ? ? ? //异或门
  54. ? ? ? ? var xorGate = new XorGate("x01")
  55. ? ? ? ? assertFalse(xorGate.getOut())
  56.  
  57. ? ? ? ? xorGate.updateIn1(true)
  58. ? ? ? ? assertTrue(xorGate.getOut())
  59.  
  60. ? ? ? ? xorGate.updateIn2(true)
  61. ? ? ? ? assertFalse(xorGate.getOut())
  62.  
  63. ? ? ? ? xorGate.updateBothIn(false, true)
  64. ? ? ? ? assertTrue(xorGate.getOut())
  65.  
  66. ? ? </script>
  67. ? ? <script>
  68. ? ? ? ? //半加器
  69. ? ? ? ? var ha = new HalfAdder("h01")
  70. ? ? ? ? assertFalse(ha.getOutS())
  71. ? ? ? ? assertFalse(ha.getOutC())
  72. ? ? ? ??
  73. ? ? ? ? ha.updateInB(true)
  74. ? ? ? ? assertTrue(ha.getOutS())
  75. ? ? ? ? assertFalse(ha.getOutC())
  76.  
  77. ? ? ? ? ha.updateInA(true)
  78. ? ? ? ? assertFalse(ha.getOutS())
  79. ? ? ? ? assertTrue(ha.getOutC())
  80.  
  81. ? ? ? ? ha.updateBothIn(true, false)
  82. ? ? ? ? assertTrue(ha.getOutS())
  83. ? ? ? ? assertFalse(ha.getOutC())
  84. ? ? </script>
  85.  
  86. ? ? <script>
  87. ? ? ? ? //全加器
  88. ? ? ? ? var fa = new FullAdder("fa01")
  89. ? ? ? ? assertFalse(fa.getOutC())
  90. ? ? ? ? assertFalse(fa.getOutS())
  91.  
  92. ? ? ? ? function test(inA, inB, inC, expectedS, expectedC){
  93. ? ? ? ? ? ? fa.updateThreeIn(inA, inB, inC)
  94. ? ? ? ? ? ? if(expectedS)
  95. ? ? ? ? ? ? ? ? assertTrue(fa.getOutS())
  96. ? ? ? ? ? ? else
  97. ? ? ? ? ? ? ? ? assertFalse(fa.getOutS())
  98.  
  99. ? ? ? ? ? ? if(expectedC)
  100. ? ? ? ? ? ? ? ? assertTrue(fa.getOutC())
  101. ? ? ? ? ? ? else
  102. ? ? ? ? ? ? ? ? assertFalse(fa.getOutC())
  103. ? ? ? ? }
  104.  
  105. ? ? ? ? test(false, false, false, false, false)
  106. ? ? ? ? test(false, true, false, true, false)
  107. ? ? ? ? test(true, false, false, true, false)
  108. ? ? ? ? test(true, true, false, false, true)
  109. ? ? ? ? test(false, false, true, true, false)
  110. ? ? ? ? test(false, true, true, false, true)
  111. ? ? ? ? test(true, false, true, false, true)
  112. ? ? ? ? test(true, true, true, true, true)
  113. ? ? ? ??
  114. ? ? </script>
  115.  
  116. ? ? <script>
  117. ? ? ? ? for(var i = 0 ; i < 256; i++){
  118. ? ? ? ? ? ? assertIntEquals(i, lowEightBitArray2Int(int2LowEightBitArray(i)))
  119. ? ? ? ? }
  120. ? ? </script>
  121. ? ? <script>
  122. ? ? ? ? var ebba = new EightBitBinaryAdder(true)
  123.  
  124. ? ? ? ? for(var i = 0; i < 256; i++){
  125. ? ? ? ? ? ? for(var j = 0; j < 256; j++){i
  126. ? ? ? ? ? ? ? ? ebba.updateBothIn(int2LowEightBitArray(i), int2LowEightBitArray(j))
  127. ? ? ? ? ? ? ? ? assertIntEquals(i + j, lowNineBitArray2Int(ebba.getOut()))
  128. ? ? ? ? ? ? }
  129. ? ? ? ? }
  130.  
  131. ? ? </script>
  132. ? ? <script>
  133. ? ? ? ? for(var i = -128; i <= 127; i++){
  134. ? ? ? ? ? ? assertIntEquals(i, eightBitArray2Int(int2EightBitArray(i)))
  135. ? ? ? ? }
  136. ? ? </script>
  137. ? ? <script>
  138. ? ? ? ? var ebba = new EightBitBinaryAdder()
  139.  
  140. ? ? ? ? for(var i = -64; i < 64; i++){
  141. ? ? ? ? ? ? for(var j = -64; j < 64; j++){
  142. ? ? ? ? ? ? ? ? ebba.updateBothIn(int2EightBitArray(i), int2EightBitArray(j))
  143. ? ? ? ? ? ? ? ? assertIntEquals(i + j, eightBitArray2Int(ebba.getOut()))
  144. ? ? ? ? ? ? }
  145. ? ? ? ? }
  146. ? ? </script>
  147. ? ? <script>
  148. ? ? ? ? for(var i = -(1<<15); i <= (1<<15) - 1; i++){
  149. ? ? ? ? ? ? assertIntEquals(i, sixteentBitArray2Int(int2SixteenBitArray(i)))
  150. ? ? ? ? }
  151. ? ? </script>
  152.  
  153. ? ? <script>
  154. ? ? ? ? var sbba = new SixteenBitBinaryAdder()
  155.  
  156. ? ? ? ? sbba.updateBothIn(int2SixteenBitArray(1156), int2SixteenBitArray(9999))
  157. ? ? ? ? assertIntEquals(9999 + 1156, sixteentBitArray2Int(sbba.getOut()))
  158.  
  159. ? ? ? ? //?16384? * ?16384? = 268435456 如果这样算会十分耗时
  160. ? ? ? ? // for(var i = -(1<<14); i < (1<<14); i++){
  161. ? ? ? ? // ? ? for(var j = -(1<<14); j < (1<<14); j++){
  162. ? ? ? ? // ? ? ? ? sbba.updateBothIn(int2SixteenBitArray(i), int2SixteenBitArray(j))
  163. ? ? ? ? // ? ? ? ? assertIntEquals(i + j, sixteentBitArray2Int(sbba.getOut()))
  164. ? ? ? ? // ? ? }
  165. ? ? ? ? // }
  166. ? ? ? ? for(var i = -100; i < 100; i++){
  167. ? ? ? ? ? ? for(var j = -100; j < 100; j++){
  168. ? ? ? ? ? ? ? ? sbba.updateBothIn(int2SixteenBitArray(i), int2SixteenBitArray(j))
  169. ? ? ? ? ? ? ? ? assertIntEquals(i + j, sixteentBitArray2Int(sbba.getOut()))
  170. ? ? ? ? ? ? }
  171. ? ? ? ? }
  172. ? ? </script>
  173. </body>
  174. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号