1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HDLAdvent Calendar 2015

Day 2

GaucheでVerilogの16進表記を作る

Posted at

Gaucheを使ってVerilogソースを作る時の小ネタです。

(define dec->verilolg-hex-str
  (lambda (d bit)
    (let* ([dispwidth (* (exact (ceiling (/ bit 8))) 2)]
           [val-str (number->string (logand (- (power 2 bit) 1) d) 16)]
           )      
      (format #f (string-append "~A'h~" (x->string dispwidth) ",,,'0@A") bit val-str))))

(dec->verilolg-hex-str 10進の数値 bit幅)で呼び出してください。

(dec->verilolg-hex-str 10 15)
gosh> "15'h000a"

(dec->verilolg-hex-str -10 15)
gosh> "15'h7ff6"
1
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?