{"action":"opened","issue":{"url":"https://api.github.com/repos/vhelin/wla-dx/issues/104","repository_url":"https://api.github.com/repos/vhelin/wla-dx","labels_url":"https://api.github.com/repos/vhelin/wla-dx/issues/104/labels{/name}","comments_url":"https://api.github.com/repos/vhelin/wla-dx/issues/104/comments","events_url":"https://api.github.com/repos/vhelin/wla-dx/issues/104/events","html_url":"https://github.com/vhelin/wla-dx/issues/104","id":149249651,"number":104,"title":"[65816/?] A Definition between 2 Sections that use it causes weird behavior","user":{"login":"bazzinotti","id":2224787,"avatar_url":"https://avatars.githubusercontent.com/u/2224787?v=3","gravatar_id":"","url":"https://api.github.com/users/bazzinotti","html_url":"https://github.com/bazzinotti","followers_url":"https://api.github.com/users/bazzinotti/followers","following_url":"https://api.github.com/users/bazzinotti/following{/other_user}","gists_url":"https://api.github.com/users/bazzinotti/gists{/gist_id}","starred_url":"https://api.github.com/users/bazzinotti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bazzinotti/subscriptions","organizations_url":"https://api.github.com/users/bazzinotti/orgs","repos_url":"https://api.github.com/users/bazzinotti/repos","events_url":"https://api.github.com/users/bazzinotti/events{/privacy}","received_events_url":"https://api.github.com/users/bazzinotti/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-04-18T19:33:09Z","updated_at":"2016-04-18T19:33:09Z","closed_at":null,"body":"Reference #102 -- After searching 12 pages of \"wla dx\" -- I found http://forums.nesdev.com/viewtopic.php?f=12&t=12268&p=139715&hilit=wla+dx#p139715\r\n\r\nI managed to identify the cause and created a minimal bug_exhibition.\r\n[section_def_bug.zip](https://github.com/vhelin/wla-dx/files/224627/section_def_bug.zip)\r\n\r\n\r\nThe beginning of `main.s` details the bug:\r\n```\r\n;»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\r\n; written by bazz 2016\r\n;»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\r\n\r\n/* When a definition is defined *between* two sections\r\nwhich both use the definition, a bug occurs.\r\n\r\nIn this case, the definition is 16-bits. eg. `.DEFINE YTemp\t\t$1808`\r\n\r\nThe section *before* the def incorrectly infers the def's bit-width\r\nThe section *after* the def correctly infers the def's bit-width.\r\n\r\nThis example executes the 2nd section which does a jsr into the first section.\r\n\r\nHere is a snippet of the ROM output this bug_exhibition creates:\r\n\r\n(section *after* def)\r\n(GOOD) \t00800b sty $1808     [001808] A:0000 X:1fff Y:0055 S:1fff D:0000 DB:00 nvMxdIzC V:  0 H: 310\r\n(GOOD) \t00800e stx $180a     [00180a] A:0000 X:1fff Y:0055 S:1fff D:0000 DB:00 nvMxdIzC V:  0 H: 350\r\n\t\t008011 jsr $8016     [008016] A:0000 X:1fff Y:0055 S:1fff D:0000 DB:00 nvMxdIzC V:  0 H: 390\r\n(section *before* def)\r\n\t\t008016 php                    A:0000 X:1fff Y:0055 S:1ffd D:0000 DB:00 nvMxdIzC V:  0 H: 436\r\n\t\t008017 rep #$10               A:0000 X:1fff Y:0055 S:1ffc D:0000 DB:00 nvMxdIzC V:  0 H: 458\r\n\t\t008019 sep #$20               A:0000 X:1fff Y:0055 S:1ffc D:0000 DB:00 nvMxdIzC V:  0 H: 480\r\n(BAD)\t00801b ldy $08       [000008] A:0000 X:1fff Y:0055 S:1ffc D:0000 DB:00 nvMxdIzC V:  0 H: 502\r\n(BAD)\t00801d ldx $0a       [00000a]\r\n\r\n00801b should be ldy $1808, for example.\r\n*/\r\n```\r\n\r\nPossible Workarounds/Solutions:\r\n- Place definitions *before* all sections that will use it\r\n- Explicit hinting (eg. ldy.w)\r\n- Disallow mid-placed definitions like this\r\n- Or, Fix this bug\r\n\r\n```\r\n.MEMORYMAP\r\nDEFAULTSLOT 0\r\nSLOTSIZE $8000\r\nSLOT 0 $8000\r\n.ENDME\r\n\r\n.ROMBANKMAP\r\nBANKSTOTAL 1\r\nBANKSIZE $8000\r\nBANKS 1\r\n.ENDRO\r\n\r\n.BANK 0 SLOT 0\r\n; === Interrupt Vector Table ====================\r\n.ORG    $7FE4   \t\t\t  ; === Native Mode ===\r\n.DW     EmptyHandler          ; COP\r\n.DW     EmptyHandler          ; BRK\r\n.DW     EmptyHandler          ; ABORT\r\n.DW     VBlank                ; NMI\r\n.DW     $0000                 ; (Unused)\r\n.DW     EmptyHandler          ; IRQ\r\n\r\n.ORG    $7FF4   ; === Emulation Mode ===\r\n.DW     EmptyHandler          ; COP\r\n.DW     $0000                 ; (Unused)\r\n.DW     EmptyHandler          ; ABORT\r\n.DW     EmptyHandler          ; NMI\r\n.DW     main                  ; RESET\r\n.DW     EmptyHandler          ; IRQ/BRK\r\n\r\n\r\n;»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\r\n; main\r\n;»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\r\n; Works OK here\r\n; .DEFINE YTemp\t\t$1808\r\n; .DEFINE XTemp\t\t$180A\r\n\r\n.BANK 0 SLOT 0\r\n.ORG 0\r\n.SECTION \"WalkerCode\"\r\n\r\nstart_metasprite:\r\n\tphp\r\n\trep #$10\r\n\tsep #$20\r\n\tldy YTemp\r\n\tldx XTemp\r\n\tplp\t\t;no clue\r\n\trts\t\t;jump back to the main code\r\n.ENDS\r\n\r\n; Works BAD here\r\n.DEFINE YTemp\t\t$1808\r\n.DEFINE XTemp\t\t$180A\r\n\r\n.BANK 0 SLOT 0\r\n.ORG 0\r\n.SECTION \"MetaspriteCode\"\r\n\r\nmain:\r\n\tsei                     ;disable interrupts\r\n\tclc                     ;switch to native mode\r\n\txce\r\n\trep #$38\r\n\tsep #$20\r\n\tldx #$1FFF\r\n\ttxs\r\n\r\n\tsty YTemp\t\t;store y to YTemp\r\n\tstx XTemp\t\t;store x to XTemp\r\n\tjsr start_metasprite\t;jump to start_metasprite to build a metasprite\r\n\r\nEmptyHandler:\r\n\trti\r\nVBlank:\r\n\trti\r\n.ENDS\r\n```"}}