From 90318f861b20587db3246c2c2344c221ab769ad8 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Fri, 24 Jun 2022 14:36:51 -0700 Subject: [PATCH] Add EEPROM image generator. --- cmd/eig/main.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 cmd/eig/main.go diff --git a/cmd/eig/main.go b/cmd/eig/main.go new file mode 100644 index 0000000..a79ecfd --- /dev/null +++ b/cmd/eig/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "os" + + "git.sr.ht/~kisom/goutils/die" +) + +// size of a kilobit in bytes +const kilobit = 128 +const pageSize = 4096 + +func main() { + size := flag.Int("s", 256*kilobit, "size of EEPROM image in kilobits") + fill := flag.Uint("f", 0, "byte to fill image with") + flag.Parse() + + if *fill > 256 { + die.With("`fill` argument must be a byte value") + } + + path := "eeprom.img" + + if flag.NArg() > 0 { + path = flag.Arg(0) + } + + fillByte := uint8(*fill) + + buf := make([]byte, pageSize) + for i := 0; i < pageSize; i++ { + buf[i] = fillByte + } + + pages := *size / pageSize + last := *size % pageSize + + file, err := os.Create(path) + die.If(err) + defer file.Close() + + for i := 0; i < pages; i++ { + _, err = file.Write(buf) + die.If(err) + } + + if last != 0 { + _, err = file.Write(buf[:last]) + die.If(err) + } +}