debugger: add back the values printer

This commit is contained in:
David Anderson 2024-09-19 23:52:45 -07:00
parent 9ad69d3e3c
commit e87e652efa
2 changed files with 71 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package memory
import (
"encoding/binary"
"fmt"
"io"
"sync"
@ -112,6 +113,32 @@ func (m *Memory) Slice(start, end int) []MemoryByte {
return ret
}
func (m *Memory) ByteSlice(start, end int) []byte {
ret := make([]byte, end-start)
for i, b := range m.Slice(start, end) {
if !b.Valid {
return nil
}
ret[i] = b.Value
}
return ret
}
func (m *Memory) Decode(addr int, out any) bool {
bs := make([]byte, binary.Size(out))
mem := m.Slice(addr, addr+len(bs))
for i, b := range mem {
if !b.Valid {
return false
}
bs[i] = b.Value
}
if _, err := binary.Decode(bs, binary.BigEndian, out); err != nil {
return false
}
return true
}
func (m *Memory) Write(addr int, val byte) {
m.mu.Lock()
defer m.mu.Unlock()

View File

@ -1,7 +1,9 @@
package main
import (
"crypto/rand"
"fmt"
"strings"
"git.sentinel65x.com/dave/gary/debugger/memory"
tea "github.com/charmbracelet/bubbletea"
@ -133,6 +135,13 @@ func (m debugger) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.readMemory(start, end-start)
case "R":
return m, m.readFullMemory()
case "p":
bs := make([]byte, 19)
rand.Read(bs)
for i, b := range bs {
m.mem.Write(m.hex.SelectedAddr()+i, b)
}
return m, m.commitWrites()
}
case msgErr:
m.lastErr = msg.err
@ -145,27 +154,38 @@ func (m debugger) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}
// func (m debugger) values(addr int) string {
// var v [4]byte
// copy(v[:], m.vram[addr:])
// var out strings.Builder
// out.WriteString(text.Render("Bin: "))
// out.WriteString(renderBytes(v[:], "%08b", "_"))
// out.WriteByte('\n')
// out.WriteString(text.Render("Hex: "))
// out.WriteString(renderBytes(v[:], "%02x", "_"))
// out.WriteByte('\n')
// out.WriteString(text.Render("Dec: "))
// out.WriteString(faintText.Render("["))
// out.WriteString(dimZero(fmt.Sprintf("%3d", v[0])))
// out.WriteString(faintText.Render("] ["))
// out.WriteString(dimZero(fmt.Sprintf("%5d", binary.BigEndian.Uint16(v[:]))))
// out.WriteString(faintText.Render("] ["))
// out.WriteString(dimZero(fmt.Sprintf("%10d", binary.BigEndian.Uint32(v[:]))))
// out.WriteString(faintText.Render("]"))
func (m debugger) values(addr int) string {
var out strings.Builder
// return out.String()
// }
k := func(k string) {
out.WriteString(text.Bold(true).Render(k) + text.Render(": "))
}
v := func(format string, args ...any) {
val := fmt.Sprintf(format, args...)
out.WriteString(text.Render(val) + "\n")
}
b := m.mem.At(addr)
k(" addr")
v("0x%x", addr)
k("binary")
v("%s", b.Bin())
k(" uint8")
v("0x%s (%s)", b.Hex(), b.Dec())
var w uint16
if m.mem.Decode(addr, &w) {
k("uint16")
v("0x%04x (%d)", w, w)
}
var dw uint32
if m.mem.Decode(addr, &dw) {
k("uint32")
v("0x%08x (%d)", dw, dw)
}
out.WriteByte('\n')
return strings.TrimSpace(out.String())
}
func (m debugger) View() string {
if m.width < 80 || m.height < 20 {
@ -173,8 +193,10 @@ func (m debugger) View() string {
}
hex := box.Render(m.hex.View())
decoder := box.Width(m.width - lip.Width(hex) - 2).Render(m.values(m.hex.SelectedAddr()))
main := lip.JoinHorizontal(lip.Top, hex, decoder)
barText := text.Reverse(true) //.Height(1)
barText := text.Reverse(true)
topLeft := barText.Bold(true).PaddingLeft(1).Render("Addr: ")
topLeft += barText.Render(fmt.Sprintf("0x%x (%d)", m.hex.SelectedAddr(), m.hex.SelectedAddr()))
@ -189,6 +211,6 @@ func (m debugger) View() string {
status := lip.JoinHorizontal(lip.Top, topLeft, title, topRight)
all := lip.JoinVertical(lip.Center, status, hex)
all := lip.JoinVertical(lip.Center, status, main)
return all
}