[chisel Tutorial] Accumulator.scala

YumeIroVillain·2023년 8월 7일
0

Chisel 독학

목록 보기
14/44
post-custom-banner

Chisel Tutorial 상에서는
총 22개의 예제가 제공되고있고, 이것을 전부 풀어보려고 한다.

실행법은 아래 내 포스트를 참고

-rw-rw-r-- 1 user user  404 Jul 23 20:24 Accumulator.scala
-rw-rw-r-- 1 user user  376 Jul 23 20:24 Adder.scala
-rw-rw-r-- 1 user user  648 Jul 23 20:24 Counter.scala
-rw-rw-r-- 1 user user 1.3K Jul 23 20:24 DynamicMemorySearch.scala
-rw-rw-r-- 1 user user  564 Jul 23 20:24 LFSR16.scala
-rw-rw-r-- 1 user user  413 Jul 23 20:24 Max2.scala
-rw-rw-r-- 1 user user  457 Jul 23 20:24 MaxN.scala
-rw-rw-r-- 1 user user  742 Jul 23 20:24 Memo.scala
-rw-rw-r-- 1 user user  641 Jul 23 20:24 Mul.scala
-rw-rw-r-- 1 user user 1.2K Jul 23 20:24 Mux4.scala
-rw-rw-r-- 1 user user  906 Jul 23 20:24 RealGCD.scala
-rw-rw-r-- 1 user user 1.1K Jul 23 20:24 SingleEvenFilter.scala
-rw-rw-r-- 1 user user  833 Jul 23 20:24 VecShiftRegister.scala
-rw-rw-r-- 1 user user  603 Jul 23 20:24 VecShiftRegisterParam.scala
-rw-rw-r-- 1 user user  541 Jul 23 20:24 VecShiftRegisterSimple.scala
-rw-rw-r-- 1 user user 1.2K Jul 23 20:24 VendingMachine.scala
-rw-rw-r-- 1 user user 1.2K Jul 23 20:24 VendingMachineSwitch.scala

Accumulator.scala

// See LICENSE.txt for license details.
package problems

import chisel3._

// Problem:
//
// Count incoming trues
// (increase counter every clock if 'in' is asserted)
//
class Accumulator extends Module {
	val io = IO(
		new Bundle {
			val in  = Input(UInt(1.W))
			val out = Output(UInt(8.W))
		}
	)

  // Implement below ----------
	val counter = RegInit(0.U(8.W))
	counter := counter + io.in
	io.out := counter
	
	printf("Accumulator: %d %d\n", io.in, io.out)
	println(s"io.out Type: ${io.out.getClass.getSimpleName}")

}

AccumulatorTest.scala

// See LICENSE.txt for license details.
package problems

import chisel3.iotesters.PeekPokeTester

class AccumulatorTests(c: Accumulator) extends PeekPokeTester(c) {
  var tot = 0
  for (t <- 0 until 16) {
    val in = rnd.nextInt(2)
    poke(c.io.in, in)
    step(1)
    if (in == 1) tot += 1

    val out = peek(c.io.out).toInt
    printf("out: %d\n", out)

    expect(c.io.out, tot)
	  // println("hello world!!!")
    // println(s"val in: $in")
    // println(s"c.io.out: $c.io.out")
    // println("c.io.out: " + c.io.out.peekInt())

    // printf("Accumulator: %d %d\n", c.io.in, c.io.out)
    // println(s"c.io.out Type: ${c.io.out.getClass.getSimpleName}")

  }
}

  • io.in 만큼, Accumulator가 다음 사이클에 증가하는 것을 확인할 수 있다

What I learned

  • Register 선언 및 0 초기화는
    RegInit(0.U(레지스터길이.W))
    로 하면 된다.
  • Chiasel Datatype 간의 assignment는 := 를 사용한다.
  • println(s"io.out Type: ${io.out.getClass.getSimpleName}")
    구문은 왜 출력되지 않는 것인지, 의문이다.
profile
HW SW 둘다 공부하는 혼종의 넋두리 블로그 / SKKU SSE 17 / SWM 11th
post-custom-banner

0개의 댓글