1
0
mirror of https://github.com/zellyn/go6502.git synced 2024-06-25 12:29:33 +00:00
go6502/asm/flavors/tests/assemble_test.go

290 lines
6.4 KiB
Go
Raw Normal View History

2014-03-05 01:42:51 +00:00
package flavors
import (
"encoding/hex"
2014-05-22 04:44:23 +00:00
"reflect"
2014-03-05 01:42:51 +00:00
"strings"
"testing"
"github.com/zellyn/go6502/asm"
2014-06-03 15:46:49 +00:00
"github.com/zellyn/go6502/asm/flavors/redbook"
2014-03-05 01:42:51 +00:00
"github.com/zellyn/go6502/asm/flavors/scma"
"github.com/zellyn/go6502/asm/lines"
2014-05-22 04:44:23 +00:00
"github.com/zellyn/go6502/asm/membuf"
2014-03-05 01:42:51 +00:00
)
2014-05-22 04:44:23 +00:00
// h converts from hex or panics.
func h(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
2014-03-05 01:42:51 +00:00
func TestMultiline(t *testing.T) {
o := lines.NewTestOpener()
ss := asm.NewAssembler(scma.New(), o)
2014-06-03 15:46:49 +00:00
rb := asm.NewAssembler(redbook.New(), o)
2014-03-05 01:42:51 +00:00
// aa := asm.NewAssembler(as65.New(), o)
// mm := asm.NewAssembler(merlin.New(), o)
tests := []struct {
a *asm.Assembler // assembler
name string // name
i []string // main file: lines
ii map[string][]string // other files: lines
b string // bytes, expected
2014-05-22 04:44:23 +00:00
ps []membuf.Piece // output bytes expected
2014-03-05 01:42:51 +00:00
active bool
}{
// We cannot determine L2, so we go wide.
{ss, "Unknown label: wide", []string{
"L1 LDA L2-L1",
"L2 NOP",
2014-05-22 04:44:23 +00:00
}, nil, "ad0300ea", nil, true},
2014-03-05 01:42:51 +00:00
// sc-asm sets instruction widths on the first pass
{ss, "Later label: wide", []string{
" LDA FOO",
"FOO .EQ $FF",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "adff00ea", nil, true},
2014-03-05 01:42:51 +00:00
// Sub-labels
{ss, "Sublabels", []string{
"L1 BEQ .1",
".1 NOP",
"L2 BEQ .2",
".1 NOP",
".2 NOP",
2014-05-22 04:44:23 +00:00
}, nil, "f000eaf001eaea", nil, true},
2014-03-05 01:42:51 +00:00
// Includes: one level deep
{ss, "Include A", []string{
" BEQ OVER",
" .IN SUBFILE1",
"OVER NOP",
}, map[string][]string{
"SUBFILE1": {
" LDA #$2a",
},
2014-05-22 04:44:23 +00:00
}, "f002a92aea", nil, true},
2014-03-05 01:42:51 +00:00
// Ifdefs: simple at first
{ss, "Ifdef A", []string{
"L1 .EQ $17",
" .DO L1>$16",
" LDA #$01",
" .ELSE",
" LDA #$02",
" .FIN",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "a901ea", nil, true},
2014-03-05 01:42:51 +00:00
// Ifdefs: else part
{ss, "Ifdef B", []string{
"L1 .EQ $16",
" .DO L1>$16",
" LDA #$01",
" .ELSE",
" LDA #$02",
" .FIN",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "a902ea", nil, true},
2014-03-05 01:42:51 +00:00
// Ifdefs: multiple else, true
{ss, "Ifdef C", []string{
"L1 .EQ $17",
" .DO L1>$16",
" LDA #$01",
" .ELSE",
" LDA #$02",
" .ELSE",
" LDA #$03",
" .ELSE",
" LDA #$04",
" .FIN",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "a901a903ea", nil, true},
2014-03-05 01:42:51 +00:00
// Ifdefs: multiple else, false
{ss, "Ifdef D", []string{
"L1 .EQ $16",
" .DO L1>$16",
" LDA #$01",
" .ELSE",
" LDA #$02",
" .ELSE",
" LDA #$03",
" .ELSE",
" LDA #$04",
" .FIN",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "a902a904ea", nil, true},
2014-03-05 01:42:51 +00:00
// Ifdef based on org, true
{ss, "Ifdef/org A", []string{
" .OR $1000",
" LDA #$01",
" .DO *=$1002",
" LDA #$02",
" .ELSE",
" LDA #$03",
" .FIN",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "a901a902ea", nil, true},
2014-03-05 01:42:51 +00:00
// Ifdef based on org, false
{ss, "Ifdef/org B", []string{
" .OR $1000",
" LDA #$01",
" .DO *=$1003",
" LDA #$02",
" .ELSE",
" LDA #$03",
" .FIN",
" NOP",
2014-05-22 04:44:23 +00:00
}, nil, "a901a903ea", nil, true},
2014-05-08 00:44:03 +00:00
// Macro, simple
{ss, "Macro, simple", []string{
" .MA INCD MACRO NAME",
" INC ]1 CALL PARAMETER",
" BNE :1 PRIVATE LABEL",
2014-05-08 23:44:08 +00:00
" INC ]1+1",
2014-05-08 00:44:03 +00:00
":1",
" .EM END OF DEFINITION",
" >INCD PTR",
"PTR .HS 0000",
"ZPTR .EQ $42",
" >INCD ZPTR",
2014-05-22 04:44:23 +00:00
}, nil, "ee0808d003ee09080000e642d002e643", nil, true},
2014-05-08 00:44:03 +00:00
// Macro, conditional assembly
{ss, "Macro, conditional assembly", []string{
" *--------------------------------",
" * DEMONSTRATE CONDITIONAL ASSEMBLY IN",
" *-------------------------------- MACRO",
" .MA INCD",
" .DO ]#=2",
" INC ]1,]2",
" BNE :3",
" INC ]1+1,]2",
":3",
" .ELSE",
" INC ]1",
" BNE :3",
" INC ]1+1",
":3",
" .FIN",
" .EM",
" *--------------------------------",
" >INCD $12",
" >INCD $1234",
" >INCD $12,X",
" >INCD $1234,X",
2014-05-22 04:44:23 +00:00
}, nil, "e612d002e613ee3412d003ee3512f612d002f613fe3412d003fe3512", nil, true},
2014-05-08 00:44:03 +00:00
// Macros, nested
{ss, "Macros, nested", []string{
" .MA CALL",
" JSR ]1",
" .DO ]#>1",
" JSR ]2",
" .FIN",
" .DO ]#>2",
" JSR ]3",
" .FIN",
" .EM",
" >CALL SAM,TOM,JOE",
" >CALL SAM,TOM",
"SAM RTS",
"JOE RTS",
"TOM RTS",
2014-05-22 04:44:23 +00:00
}, nil, "200f08201108201008200f08201108606060", nil, true},
// Check outputs when origin is changed.
{ss, "Origin A", []string{
" .OR $1000",
" LDA #$01",
" .OR $2000",
" LDA #$02",
" .OR $1002",
" LDA #$03",
" NOP",
}, nil, "a901a902a903ea", []membuf.Piece{
{0x1000, h("a901a903ea")},
{0x2000, h("a902")},
}, true},
2014-06-03 15:46:49 +00:00
// Check turning MSB on and off
{rb, "MSB toggle", []string{
" ASC 'AB'",
" MSB OFF",
" ASC 'AB'",
" MSB ON",
" ASC 'AB'",
}, nil, "c1c24142c1c2", nil, true},
2014-03-05 01:42:51 +00:00
}
for i, tt := range tests {
if !tt.active {
continue
}
2014-05-22 04:44:23 +00:00
if tt.b == "" && len(tt.ps) == 0 {
t.Fatalf(`%d("%s"): test case must specify bytes or pieces`, i, tt.name)
}
2014-03-05 01:42:51 +00:00
tt.a.Reset()
o.Clear()
o["TESTFILE"] = strings.Join(tt.i, "\n")
for k, v := range tt.ii {
o[k] = strings.Join(v, "\n")
}
2014-06-03 15:46:49 +00:00
if err := tt.a.Load("TESTFILE", 0); err != nil {
2014-03-05 01:42:51 +00:00
t.Errorf(`%d("%s"): tt.a.Load("TESTFILE") failed: %s`, i, tt.name, err)
continue
}
2014-05-08 00:44:03 +00:00
if !tt.a.Flavor.SetWidthsOnFirstPass() {
if _, err := tt.a.Pass(true, false); err != nil {
t.Errorf(`%d("%s"): tt.a.Pass(true, false) failed: %s`, i, tt.name, err)
continue
}
2014-03-05 01:42:51 +00:00
}
isFinal, err := tt.a.Pass(true, true)
if err != nil {
t.Errorf(`%d("%s"): tt.a.Pass(true, true) failed: %s`, i, tt.name, err)
continue
}
if !isFinal {
t.Errorf(`%d("%s"): tt.a.Pass(true, true) couldn't finalize`, i, tt.name)
continue
}
2014-05-22 04:44:23 +00:00
if tt.b != "" {
bb, err := tt.a.RawBytes()
if err != nil {
t.Errorf(`%d("%s"): tt.a.RawBytes() failed: %s`, i, tt.name, err)
continue
}
hx := hex.EncodeToString(bb)
if hx != tt.b {
t.Errorf(`%d("%s"): tt.a.RawBytes()=[%s]; want [%s]`, i, tt.name, hx, tt.b)
continue
}
2014-03-05 01:42:51 +00:00
}
2014-05-22 04:44:23 +00:00
if len(tt.ps) != 0 {
m, err := tt.a.Membuf()
if err != nil {
t.Errorf(`%d("%s"): tt.a.Membuf() failed: %s`, i, tt.name, err)
continue
}
ps := m.Pieces()
if !reflect.DeepEqual(ps, tt.ps) {
t.Errorf(`%d("%s"): tt.Membuf().Pieces() = %v; want %v`, i, tt.name, ps, tt.ps)
}
2014-03-05 01:42:51 +00:00
}
}
}