aboutsummaryrefslogtreecommitdiff
path: root/liust-50/cmd/liustsim/simulator.go
diff options
context:
space:
mode:
Diffstat (limited to 'liust-50/cmd/liustsim/simulator.go')
-rw-r--r--liust-50/cmd/liustsim/simulator.go325
1 files changed, 325 insertions, 0 deletions
diff --git a/liust-50/cmd/liustsim/simulator.go b/liust-50/cmd/liustsim/simulator.go
new file mode 100644
index 0000000..43e06d9
--- /dev/null
+++ b/liust-50/cmd/liustsim/simulator.go
@@ -0,0 +1,325 @@
+package main
+
+import (
+ "bufio"
+ "image"
+ "image/color"
+ "os"
+ "strconv"
+ "strings"
+
+ "fyne.io/fyne/v2"
+ "fyne.io/fyne/v2/app"
+ "fyne.io/fyne/v2/canvas"
+
+ "janouch.name/desktop-tools/liust-50/charset"
+)
+
+const (
+ displayWidth = 20
+ displayHeight = 2
+ charWidth = 5 + 1
+ charHeight = 7 + 1
+ magnification = 5
+)
+
+// TODO(p): See how this works exactly, and implement it.
+const (
+ cursorModeOff = iota
+ cursorModeBlink
+ cursorModeLightUp
+)
+
+type Display struct {
+ image *canvas.Image
+ chars [displayHeight][displayWidth]uint8
+ charset uint8
+ cursorX int
+ cursorY int
+ cursorMode int
+}
+
+func NewDisplay() *Display {
+ return &Display{charset: 2}
+}
+
+func (d *Display) Clear() {
+ for y := 0; y < displayHeight; y++ {
+ for x := 0; x < displayWidth; x++ {
+ d.chars[y][x] = 0x20 // space
+ }
+ }
+}
+
+func (d *Display) ClearToEnd() {
+ for x := d.cursorX; x < displayWidth; x++ {
+ d.chars[d.cursorY][x] = 0x20 // space
+ }
+}
+
+func (d *Display) drawCharacter(
+ img *image.RGBA, charImg image.Image, cx, cy int) {
+ if charImg == nil {
+ return
+ }
+
+ bounds := charImg.Bounds()
+ width, height := bounds.Dx(), bounds.Dy()
+ for dy := 0; dy < height; dy++ {
+ for dx := 0; dx < width; dx++ {
+ c := charImg.At(bounds.Min.X+dx, bounds.Min.Y+dy)
+ if r, _, _, _ := c.RGBA(); r >= 0x8000 {
+ c = color.RGBA{0x00, 0xFF, 0xA0, 0xFF}
+ } else {
+ c = color.RGBA{0x20, 0x20, 0x20, 0xFF}
+ }
+ img.Set(1+cx*charWidth+dx, 1+cy*charHeight+dy, c)
+ }
+ }
+}
+
+func (d *Display) Render() image.Image {
+ width := 1 + displayWidth*charWidth
+ height := 1 + displayHeight*charHeight
+
+ img := image.NewRGBA(image.Rect(0, 0, width, height))
+ for y := 0; y < height; y++ {
+ for x := 0; x < width; x++ {
+ img.Set(x, y, color.Black)
+ }
+ }
+
+ for cy := 0; cy < displayHeight; cy++ {
+ for cx := 0; cx < displayWidth; cx++ {
+ charImg := charset.ResolveCharToImage(d.chars[cy][cx], d.charset)
+ d.drawCharacter(img, charImg, cx, cy)
+ }
+ }
+
+ return img
+}
+
+func (d *Display) PutChar(ch uint8) {
+ if d.cursorX >= displayWidth || d.cursorY >= displayHeight {
+ return
+ }
+
+ d.chars[d.cursorY][d.cursorX] = ch
+ d.cursorX++
+ if d.cursorX >= displayWidth {
+ d.cursorX = displayWidth - 1
+ }
+}
+
+func (d *Display) LineFeed() {
+ d.cursorY++
+ if d.cursorY >= displayHeight {
+ d.cursorY = displayHeight - 1
+
+ // XXX: This relies on displayHeight being exactly 2.
+ d.chars[0] = d.chars[1]
+ for x := 0; x < displayWidth; x++ {
+ d.chars[1][x] = 0x20
+ }
+ }
+}
+
+func (d *Display) CarriageReturn() {
+ d.cursorX = 0
+}
+
+func (d *Display) Backspace() {
+ if d.cursorX > 0 {
+ d.cursorX--
+ }
+}
+
+func (d *Display) SetCursor(x, y int) {
+ if x >= 0 && x < displayWidth {
+ d.cursorX = x
+ }
+ if y >= 0 && y < displayHeight {
+ d.cursorY = y
+ }
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+func parseANSI(input string) (command string, params []int) {
+ if !strings.HasPrefix(input, "\x1b[") {
+ return "", nil
+ }
+
+ input = input[2:]
+ if len(input) == 0 {
+ return "", nil
+ }
+
+ cmdIdx := len(input) - 1
+ command = string(input[cmdIdx])
+
+ paramStr := input[:cmdIdx]
+ if paramStr == "" {
+ return command, []int{}
+ }
+
+ parts := strings.Split(paramStr, ";")
+ for _, p := range parts {
+ p = strings.TrimSpace(p)
+ if val, err := strconv.Atoi(p); err == nil {
+ params = append(params, val)
+ }
+ }
+ return command, params
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+type escapeParser struct {
+ seq strings.Builder
+ inEsc bool
+ inCSI bool
+ display *Display
+}
+
+func newEscapeParser(d *Display) *escapeParser {
+ return &escapeParser{display: d}
+}
+
+func (ep *escapeParser) reset() {
+ ep.inEsc = false
+ ep.inCSI = false
+ ep.seq.Reset()
+}
+
+func (ep *escapeParser) handleCSICommand() bool {
+ cmd, params := parseANSI(ep.seq.String())
+
+ switch cmd {
+ case "J": // Clear display
+ // XXX: No params case is unverified.
+ if len(params) == 0 || params[0] == 2 {
+ ep.display.Clear()
+ }
+ case "K": // Delete to end of line
+ // XXX: No params case is unverified (but it should work).
+ if len(params) == 0 || params[0] == 0 {
+ ep.display.ClearToEnd()
+ }
+ case "H": // Cursor position
+ y, x := 0, 0
+ if len(params) >= 1 {
+ y = params[0] - 1 // 1-indexed to 0-indexed
+ }
+ if len(params) >= 2 {
+ x = params[1] - 1
+ }
+ ep.display.SetCursor(x, y)
+ }
+ return true
+}
+
+func (ep *escapeParser) handleEscapeSequence(b byte) bool {
+ ep.seq.WriteByte(b)
+
+ if ep.seq.Len() == 2 && b == '[' {
+ ep.inCSI = true
+ return false
+ }
+
+ if ep.seq.Len() == 3 && ep.seq.String()[1] == 'R' {
+ ep.display.charset = b
+ ep.reset()
+ return true
+ }
+
+ if ep.inCSI && (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') {
+ refresh := ep.handleCSICommand()
+ ep.reset()
+ return refresh
+ }
+
+ if ep.seq.Len() == 6 && ep.seq.String()[1:5] == "\\?LC" {
+ ep.display.cursorMode = int(ep.seq.String()[5])
+ return true
+ }
+
+ return false
+}
+
+func (ep *escapeParser) handleControlChar(b byte) bool {
+ switch b {
+ case 0x0A: // LF
+ ep.display.LineFeed()
+ return true
+ case 0x0D: // CR
+ ep.display.CarriageReturn()
+ return true
+ case 0x08: // BS
+ ep.display.Backspace()
+ return true
+ default:
+ if b >= 0x20 {
+ ep.display.PutChar(b)
+ return true
+ }
+ }
+ return false
+}
+
+func (ep *escapeParser) handleByte(b byte) (needsRefresh bool) {
+ if b == 0x1b { // ESC
+ ep.reset()
+ ep.inEsc = true
+ ep.seq.WriteByte(b)
+ return false
+ }
+
+ if ep.inEsc {
+ return ep.handleEscapeSequence(b)
+ }
+
+ return ep.handleControlChar(b)
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+func main() {
+ myApp := app.New()
+ window := myApp.NewWindow("Toshiba Tec LIUST-50 Simulator")
+
+ display := NewDisplay()
+ display.Clear()
+
+ img := canvas.NewImageFromImage(display.Render())
+ img.FillMode = canvas.ImageFillOriginal
+ img.ScaleMode = canvas.ImageScalePixels
+
+ window.SetContent(img)
+
+ window.Resize(fyne.NewSize(600, 100))
+
+ go func() {
+ reader := bufio.NewReader(os.Stdin)
+ parser := newEscapeParser(display)
+
+ for {
+ b, err := reader.ReadByte()
+ if err != nil {
+ fyne.DoAndWait(func() {
+ window.SetTitle(err.Error())
+ })
+ return
+ }
+
+ if parser.handleByte(b) {
+ fyne.DoAndWait(func() {
+ img.Image = display.Render()
+ img.Refresh()
+ })
+ }
+ }
+ }()
+
+ window.ShowAndRun()
+}