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"