[script] Tweak CDL caching

Reworked the CDL caching to properly handle unknown bytes in scripts.
This commit is contained in:
Zorchenhimer 2025-10-26 18:33:10 -04:00
parent 63de05e9dd
commit 37711fa4bb
Signed by: Zorchenhimer
GPG Key ID: 70A1AB767AAB9C20
3 changed files with 21 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import (
"strconv"
"fmt"
"slices"
"bytes"
)
type CodeDataLog struct {
@ -166,8 +167,11 @@ func (cdl *CodeDataLog) setCode(addr int) {
cdl.cache[addr] |= cdlCode
}
func (cdl *CodeDataLog) doCache() error {
func (cdl *CodeDataLog) doCache(size int) error {
cdl.cache = make(map[int]cdlBit)
for i := 0x6000; i < size+0x6000; i++ {
cdl.cache[i] = cdlUnknown
}
for _, rng := range cdl.Code {
start, err := strconv.ParseInt(rng.Start, 0, 32)
@ -215,15 +219,18 @@ func (cdl *CodeDataLog) doCache() error {
}
func CdlFromJson(r io.Reader) (*CodeDataLog, error) {
raw, err := io.ReadAll(r)
buf := bytes.NewReader(raw)
cdl := NewCDL()
dec := json.NewDecoder(r)
err := dec.Decode(cdl)
dec := json.NewDecoder(buf)
err = dec.Decode(cdl)
if err != nil {
return nil, err
}
//cdl.Data = []CdlRange{}
cdl.doCache()
cdl.doCache(len(raw))
return cdl, nil
}

View File

@ -191,26 +191,19 @@ INNER:
}
// Add data tokens
for addr, bit := range p.script.CDL.cache {
if addr < 0x6002 {
continue
}
for addr := 0x6002; addr < len(rawinput)+0x6000; addr++ {
bit := p.script.CDL.cache[addr]
// ignore code bytes
if bit & cdlCode == cdlCode {
continue
}
// ignore labels outside the script's address range
if addr > len(rawinput)+0x6000 {
continue
}
if _, ok := p.script.Labels[addr]; ok {
p.script.Tokens = append(p.script.Tokens, &Token{
Offset: addr,
Inline: []InlineVal{NewWordVal([]byte{rawinput[addr-0x6000], rawinput[addr+1-0x6000]})},
IsVariable: true,
//Inline: []InlineVal{NewWordVal([]byte{rawinput[addr-0x6000], rawinput[addr+1-0x6000]})},
//IsVariable: true,
IsData: true,
cdl: bit.String(),
})

View File

@ -44,12 +44,12 @@ func (s *Script) DebugCDL(filename string) error {
return fmt.Errorf("origSize == 0")
}
if s.CDL.cache == nil {
err := s.CDL.doCache()
if err != nil {
return fmt.Errorf("doCache() error: %w", err)
}
}
//if s.CDL.cache == nil {
// err := s.CDL.doCache()
// if err != nil {
// return fmt.Errorf("doCache() error: %w", err)
// }
//}
dat := make([]byte, s.origSize)
for i := 2; i < len(dat); i++ {