| Season mode menu: Play Mode gets a 6th choice "Whole Season" (jdc's idea). The game itself still sees Regular Season
| ($D048 = 0); the choice is a flag in the season header in battery RAM (+12). In Whole Season the menu shows your next
| game from the schedule and only your team can be picked before the first game.
| Hooks: $F7BAA (the menu's value printer, runs for every row on every redraw) and $F7ABA (the value writer).

        .equ    SRAM_OFF, 0x1000
        .equ    MAGIC,    0x5345534E
        .equ    SRAM_RD,  0x1A244
        .equ    HDR,      0xFFE3E0       | 16-byte copy of the season header
        .equ    FORCED,   0xFFE3F0       | word: 1 while the menu is forced
        .equ    SELV,     0xFFE3F2       | word: Whole Season selected (from the header)
        .equ    NBYTES,   16+32*16
        .equ    BUF,      0xFFE000
        .equ    SUMBUF,   0xFFE300
        .equ    SRAM_WR,  0x1A1E4
        .equ    USERW,    0xFFE3F4       | word: your team, shown in the "Team" row
        .equ    NEXTK,    0xFFE3F6       | next game index (0-based)
        .equ    NEXTOPP,  0xFFE3F8       | next opponent
        .equ    NEXTAWAY, 0xFFE3FA       | 1 = away game
        .equ    GSTR,     0xFFE3A0       | "Game" row text: word 22 + 20 characters
        .equ    EXITV,    0xFFE3B8       | long: where the stats screens return to (main menu); EXITM word = $4D41 when set
        .equ    EXITM,    0xFFE3BC
        .equ    MAININIT, 0xF739E        | builds the whole main menu and runs its loop
        .equ    STANDINGS,0x2F9000
        .equ    LEADERS,  0x2FA000
        .equ    LABELS,   0xF8070        | the menu's label drawing routine
        .equ    PLAYMODE, 0xFFD048
        .equ    PLAYERS,  0xFFD04A
        .equ    HOME,     0xFFD04C
        .equ    AWAY,     0xFFD04E
        .equ    HOOK8,    0x2FE000       | patch.py fix 8 (jsr $20961A + Fighting N/A)
        .equ    WRITEVAL, 0x2095D6

        .text
        .globl  _start
_start:                                 | replaces "jsr $20961A" at $F7BAA
        bsr.w   force
        bra.w   dispsel

        .globl  writehook               | replaces "jsr $2095D6" at $F7ABA (_start + 8)
writehook:
        tst.w   %d7                     | Play Mode row: Regular Season shows as Continue Season / New Season
        bne.s   0f
        bra.w   playmode
0:      cmpi.w  #1,FORCED               | season running: everything you chose at the start stays
        beq.w   blocked
        cmpi.w  #2,%d7                  | Players row in Whole Season: One <-> Two - Teammates
        bne.s   plnot
        tst.w   SELV
        beq.s   plnot
        cmp.w   PLAYERS,%d3             | the menu writes every row again after a change: same value = no press
        beq.s   plw
        moveq   #3,%d3
        cmpi.w  #3,PLAYERS
        bne.s   plw
        moveq   #1,%d3
plw:    jmp     WRITEVAL
plnot:  tst.w   FORCED                  | 0 = not Regular Season, 1 = season running, 2 = before the first game
        beq.s   1f
        cmpi.w  #2,%d7                  | Players and Away are always locked
        beq.s   2f
        cmpi.w  #6,%d7
        beq.s   2f
        cmpi.w  #4,%d7                  | Home (your team) only before the season has started
        bne.s   1f
        cmpi.w  #1,FORCED
        beq.s   2f
1:      jmp     WRITEVAL
2:
blocked:
        tst.w   %d2                     | same flags as the original routine returns
        rts

force:  movem.l %d0-%d3/%a0-%a1,-(%sp)
        clr.w   FORCED
        clr.w   SELV
        lea     HDR,%a0
        move.l  #SRAM_OFF,%d0
        moveq   #16,%d1
        jsr     SRAM_RD
        lea     HDR,%a0
        tst.w   PLAYMODE                | Regular Season = the season
        bne.w   9f
        move.w  #1,SELV
        cmpi.l  #MAGIC,(%a0)
        bne.s   3f                      | no season block yet: before the first game
        tst.w   12(%a0)                 | New Season chosen: like before the first game
        bne.s   3f
        move.w  6(%a0),%d0              | your team + 1
        beq.s   3f
        subq.w  #1,%d0
        move.w  8(%a0),%d1              | next game index
        cmp.w   10(%a0),%d1
        bcc.w   9f                      | season over
        move.w  #1,FORCED
        bra.s   4f
3:      move.w  HOME,%d0                | before the first game: your team = Home, game 0 of its schedule
        moveq   #0,%d1
        move.w  #2,FORCED
4:
        moveq   #0,%d2
        move.w  %d1,%d2
        divu.w  #31,%d2
        swap    %d2                     | k mod 31
        lea     sched,%a1
        moveq   #0,%d3
        move.b  (%a1,%d2.w),%d3
        add.w   %d0,%d3
        andi.w  #31,%d3                 | opponent
        move.w  %d0,USERW
        move.w  %d1,NEXTK
        move.w  %d3,NEXTOPP
        clr.w   NEXTAWAY
        cmpi.w  #3,PLAYERS              | Two - Teammates always controls the home team: play every game at home
        beq.s   51f
        btst    #0,%d1
        bne.s   5f
51:     move.w  %d0,HOME                | even game: at home
        move.w  %d3,AWAY
        moveq   #1,%d2
        bra.s   6f
5:      move.w  #1,NEXTAWAY
        move.w  %d3,HOME                | odd game: away
        move.w  %d0,AWAY
        moveq   #2,%d2
6:      cmpi.w  #3,PLAYERS              | co-op stays co-op
        beq.w   9f
        move.w  %d2,PLAYERS             | you control your own team (One - Home / One - Visitor)
9:      movem.l (%sp)+,%d0-%d3/%a0-%a1
        rts

| row 0 shows "Whole Season" when chosen, everything else goes to fix 8 (jsr $20961A + Fighting N/A)
dispsel:                                | a0 = $FFD048 here (the value routine needs it): don't touch a0
        tst.w   SELV
        beq.s   1f
        tst.w   %d7                     | Play Mode in Regular Season: "Continue Season" / "New Season"
        bne.s   4f
        pea     newstr
        bsr.w   hasseason
        beq.s   2f
        tst.w   HDR+12
        bne.s   2f
        addq.l  #4,%sp
        pea     contstr
        bra.s   2f
4:      cmpi.w  #2,%d7                  | Players: "One" for One - Home / One - Visitor
        bne.s   1f
        cmpi.w  #3,PLAYERS
        beq.s   1f
        pea     onestr
2:      jsr     0x20961A
        movea.l (%sp)+,%a1
        addq.l  #4,%sp
        jmp     0x11BA4
1:      jmp     HOOK8

| team rows ($F7C72, replaces "movea.l #$FFFFD048,a1"): in Whole Season row Home shows your team, row Away is empty
        .globl  teamrow
teamrow:
        movea.l #0xFFFFD048,%a1
        tst.w   SELV
        beq.s   1f
        cmpi.w  #6,%d7
        beq.s   2f
        cmpi.w  #4,%d7
        bne.s   1f
        movea.l #USERW-4,%a1            | (a1,d7) = your team
1:      rts
2:      addq.l  #4,%sp                  | Away row: "Game  2/82 at COL" and return from the row printer
        bsr.w   gameinfo
        lea     GSTR,%a1
        jmp     0x11BA4

| GSTR = word 22 + "n/82 at ABC" / "n/82 vs ABC" padded to 20 characters
gameinfo:
        movem.l %d0-%d3/%a0-%a2,-(%sp)
        lea     GSTR,%a0
        move.w  #22,(%a0)+
        lea     20(%a0),%a2
        move.w  NEXTK,%d2
        addq.w  #1,%d2
        bsr.w   num
        move.b  #0x2F,(%a0)+            | "/"
        move.w  HDR+10,%d2
        bne.s   1f
        moveq   #82,%d2
1:      bsr.w   num
        move.b  #0x20,(%a0)+
        move.b  #0x76,(%a0)+            | "vs"
        move.b  #0x73,(%a0)+
        tst.w   NEXTAWAY
        beq.s   2f
        move.b  #0x61,-2(%a0)           | "at"
        move.b  #0x74,-1(%a0)
2:      move.b  #0x20,(%a0)+
        lea     abbr,%a1
        move.w  NEXTOPP,%d2
        mulu.w  #3,%d2
        move.b  (%a1,%d2.w),(%a0)+
        move.b  1(%a1,%d2.w),(%a0)+
        move.b  2(%a1,%d2.w),(%a0)+
3:      cmpa.l  %a2,%a0
        bge.s   4f
        move.b  #0x20,(%a0)+
        bra.s   3b
4:      movem.l (%sp)+,%d0-%d3/%a0-%a2
        rts
| d2 = 1..99 -> digits (no padding)
num:    andi.l  #0xFFFF,%d2
        divu.w  #10,%d2
        tst.w   %d2
        beq.s   1f
        addi.b  #0x30,%d2
        move.b  %d2,(%a0)+
1:      swap    %d2
        addi.b  #0x30,%d2
        move.b  %d2,(%a0)+
        rts

| label loop ($F80BC, replaces "jsr $11A48"): "Home" -> "Team", "Away" -> empty in Whole Season
        .globl  labelhook
labelhook:
        bsr.w   checksel
        tst.w   SELV
        beq.s   1f
        cmpa.l  #0x20917A,%a1
        beq.s   2f
        cmpa.l  #0x20918A,%a1
        beq.s   3f
1:      jmp     0x11A48
2:      move.l  %a1,-(%sp)
        lea     teamlbl,%a1
        bra.s   4f
3:      move.l  %a1,-(%sp)
        lea     blanklbl,%a1
4:      jsr     0x11A48
        movea.l (%sp)+,%a1
        adda.w  (%a1),%a1               | continue with the next label
        rts

| main menu Left/Right ($F75C2, replaces "cmpi.w #4,$D048"): on the Game row in Whole Season,
| Right opens Season Standings and Left opens Scoring Leaders. They come back by rebuilding the main menu.
        .globl  mainlr
mainlr:
        tst.w   SELV
        beq.s   1f
        cmpi.w  #6,%d7
        bne.s   1f
        addq.l  #4,%sp                  | leave the menu loop's frame as it is
        move.l  #mainback,EXITV
        move.w  #0x4D41,EXITM
        move.w  %d2,-(%sp)
        move.w  0xFFB026,%d4            | set up the scoreboard screen the menus are drawn on (as $130E6 does)
        movea.l #0xA892A,%a2
        jsr     0x11738
        move.w  (%sp)+,%d2
        tst.w   %d2
        bmi.s   2f
        jmp     STANDINGS
2:      jmp     LEADERS
1:      cmpi.w  #4,PLAYMODE             | the instruction we replaced (flags are used after the return)
        rts
mainback:
        clr.w   EXITM
        jmp     MAININIT

| $1732C (replaces "jsr $FCC76", the Matchups screen after Start): in Whole Season go straight to the pre-game menu,
| where Season Standings and Scoring Leaders are (jdc wanted the stats one press from the main menu).
        .globl  skipmatch
skipmatch:
        bsr.w   checksel
        tst.w   SELV
        bne.s   1f
        jmp     0xFCC76
1:      rts

| SELV from the battery header (labels are drawn before the value rows)
checksel:
        movem.l %d0-%d1/%a0-%a1,-(%sp)
        clr.w   SELV
        lea     HDR,%a0
        move.l  #SRAM_OFF,%d0
        moveq   #16,%d1
        jsr     SRAM_RD
        tst.w   PLAYMODE                | Regular Season = the season
        bne.s   1f
        move.w  #1,SELV
1:      movem.l (%sp)+,%d0-%d1/%a0-%a1
        rts

| redraw the labels after Play Mode changed (keeps the text cursor of the caller)
relabel:
        movem.l %d0-%d7/%a0-%a6,-(%sp)
        move.l  0xFFB028,-(%sp)
        move.l  0xFFB02C,-(%sp)
        jsr     LABELS
        move.l  (%sp)+,0xFFB02C
        move.l  (%sp)+,0xFFB028
        movem.l (%sp)+,%d0-%d7/%a0-%a6
        rts

| Play Mode: d3 = the game's new value (0..4, from $D048 with wrap-around). Insert Whole Season between Shootout and Regular.
playmode:
| d3 = the game's new value (from $D048 with wrap-around and its own skips). With a saved season, Regular Season (0)
| is two choices: Continue Season (header +12 = 0) and, one step to the right, New Season (+12 = 1).
        cmp.w   PLAYMODE,%d3            | the menu writes every row again after a change: same value = no press
        beq.w   pmw
        bsr.w   hasseason               | Z clear = a season is saved
        beq.s   pmdone                  | no season: just the normal choices (Regular Season shows "New Season")
        move.w  PLAYMODE,%d0
        bne.s   2f
        tst.w   HDR+12                  | from Regular Season
        bne.s   1f
        cmpi.w  #4,%d3                  | Continue + Left -> Shootout
        beq.s   pmdone
        moveq   #0,%d3                  | Continue + Right -> New Season
        moveq   #1,%d0
        bsr.w   setsel
        bra.s   pmdone
1:      cmpi.w  #4,%d3                  | New + Left -> Continue Season
        bne.s   11f
        moveq   #0,%d3
11:     moveq   #0,%d0                  | New + Right -> the playoff modes (next time it starts as Continue)
        bsr.w   setsel
        bra.s   pmdone
2:      tst.w   %d3                     | into Regular Season from another mode
        bne.s   pmdone
        moveq   #0,%d0                  | from Shootout (Right) -> Continue Season
        cmpi.w  #4,PLAYMODE
        beq.s   3f
        moveq   #1,%d0                  | from the playoff modes (Left) -> New Season
3:      bsr.w   setsel
pmdone: jsr     WRITEVAL
        bsr.w   relabel
        tst.w   %d2
        rts
pmw:    jmp     WRITEVAL

| Z clear if a season is saved (header in HDR: magic and a team)
hasseason:
        cmpi.l  #MAGIC,HDR
        bne.s   1f
        tst.w   HDR+6
        rts
1:      ori     #4,%ccr                 | Z set
        rts

| setsel: d0 = 0/1 -> header +12 in battery RAM (creates the season block if needed, keeps the checksum right)
setsel: movem.l %d0-%d7/%a0-%a1,-(%sp)
        move.w  %d0,%d5
        move.w  %d0,SELV
        lea     BUF,%a0
        move.l  #SRAM_OFF,%d0
        move.l  #NBYTES,%d1
        jsr     SRAM_RD
        bsr.w   bsum
        move.b  %d7,%d6
        lea     BUF,%a0
        cmpi.l  #MAGIC,(%a0)
        beq.s   1f
        move.w  #NBYTES/2-1,%d2
        movea.l %a0,%a1
2:      clr.w   (%a1)+
        dbra    %d2,2b
        move.l  #MAGIC,(%a0)
1:      move.w  %d5,12(%a0)
        move.w  %d5,HDR+12
        move.l  #SRAM_OFF,%d0
        move.l  #NBYTES,%d1
        jsr     SRAM_WR
        bsr.w   bsum
        lea     SUMBUF,%a0
        move.l  #0x1FFE,%d0
        moveq   #2,%d1
        jsr     SRAM_RD
        lea     SUMBUF,%a0
        move.b  1(%a0),%d0
        sub.b   %d6,%d0
        add.b   %d7,%d0
        move.b  %d0,1(%a0)
        not.b   %d0
        move.b  %d0,(%a0)
        move.l  #0x1FFE,%d0
        moveq   #2,%d1
        jsr     SRAM_WR
        movem.l (%sp)+,%d0-%d7/%a0-%a1
        rts
bsum:   lea     BUF,%a1
        moveq   #0,%d7
        move.w  #NBYTES-1,%d2
3:      add.b   (%a1)+,%d7
        dbra    %d2,3b
        rts

wholestr:
        .word   22
        .ascii  "Whole Season        "
newstr: .word   22
        .ascii  "New Season          "
contstr:
        .word   22
        .ascii  "Continue Season     "
onestr: .word   22
        .ascii  "One                 "
blankstr:
        .word   22
        .ascii  "                    "
teamlbl:
        .word   16
        .ascii  "Team          "
blanklbl:
        .word   16
        .ascii  "Game          "
abbr:   .ascii  "ANABOSBUFCGYCARCHICOLCBJDALDETEDMFLALAKMINMTLNSHNJDNYINYROTTPHIPITSJSSEASTLTBLTORUTAVANVGKWASWPG"
        .even

        .include "sched.inc"
