From b5b9637ff2d222841632b8644b94835ec1c93359 Mon Sep 17 00:00:00 2001 From: Astatin Date: Tue, 7 Jan 2025 16:20:52 +0900 Subject: Add label offset + allow to use labels with .DB macro + relative labels --- macros.go | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'macros.go') diff --git a/macros.go b/macros.go index 8e8d4c1..5daf3c9 100644 --- a/macros.go +++ b/macros.go @@ -30,7 +30,16 @@ func NewInstructionSetMacros() InstructionSet { Assembler: func(currentAddress uint16, args []uint16) ([]byte, error) { return make([]byte, args[0]-currentAddress), nil }, - MacroForbidden: true, + MacroForbidden: true, + LabelsBeforeOnly: true, + }, + { + Types: []ParamType{Raw16MacroRelativeLabel}, + Assembler: func(currentAddress uint16, args []uint16) ([]byte, error) { + return make([]byte, args[0]-currentAddress), nil + }, + MacroForbidden: false, + LabelsBeforeOnly: true, }, } @@ -46,6 +55,18 @@ func NewInstructionSetMacros() InstructionSet { }, Wildcard: true, }, + { + Types: []ParamType{Raw16}, + Assembler: func(currentAddress uint16, args []uint16) ([]byte, error) { + result := make([]byte, len(args)*2) + for i := range args { + result[i*2] = uint8(args[i] >> 8) + result[i*2+1] = uint8(args[i] & 0xff) + } + return result, nil + }, + Wildcard: true, + }, } return result @@ -63,8 +84,8 @@ func MacroParse( lines []string, result *[]byte, state *ProgramState, - line_nb *int, // line_nb - is_first_pass bool, + lineNb *int, + isFirstPass bool, offset uint, ) error { words := strings.Split(line, " ") @@ -79,6 +100,7 @@ func MacroParse( &state.Labels, &state.Defs, state.IsMacro, + isFirstPass, uint16(uint(len(*result))+offset), line, ) @@ -102,7 +124,7 @@ func MacroParse( } fileStartOffset := uint(len(*result)) + offset - if is_first_pass { + if isFirstPass { included, err := firstPass(filePath, input, fileStartOffset, state) if err != nil { return err @@ -145,14 +167,14 @@ func MacroParse( return fmt.Errorf(".MACRODEF should have one argument, followed by the definition") } definedMacroName := strings.ToUpper(words[1]) - (*line_nb) += 1 + (*lineNb) += 1 macroContent := []byte{} - for *line_nb < len(lines) && strings.TrimSpace(strings.Split(lines[*line_nb], ";")[0]) != ".END" { - macroContent = append(macroContent, (lines[*line_nb] + "\n")...) - (*line_nb) += 1 + for *lineNb < len(lines) && strings.TrimSpace(strings.Split(lines[*lineNb], ";")[0]) != ".END" { + macroContent = append(macroContent, (lines[*lineNb] + "\n")...) + (*lineNb) += 1 } - if is_first_pass { + if isFirstPass { if _, ok := MacroInstructions[definedMacroName]; ok { return fmt.Errorf("Macro %s is already defined", definedMacroName) } -- cgit v1.2.3-70-g09d2