| Season mode: the league table after every season game, drawn like the game's own playoff tree screen ($17336):
| the team name banners from the playoff tree, one division per screen (8 teams) by points, opens on your division,
| Left/Right = division, Start = on. Columns: rank in the division, banner, abbreviation, GP, PTS, and the gap to the
| division leader (the leader shows his lead over number 2).
| Hook: $17336 (tst.w $D048 / beq $15464 -> jmp _start / nop). $17336 is called after a game ($172E0, returns to
| $172E4) and before one ($1731E); in Regular Season the game only did rts there. Playoffs: the tree as before.

        .equ    SRAM_OFF, 0x1000
        .equ    NBYTES,   16+32*16
        .equ    MAGIC,    0x5345534E
        .equ    SRAM_RD,  0x1A244
        .equ    BUF,      0xFFE000       | season block
        .equ    ORDER,    0xFFE300       | 32 team indices, best first
        .equ    KEY,      0xFFE320       | 32 sort keys (points*256 + goal difference + 128)
        .equ    PAGE,     0xFFE360       | division on screen (0 Atlantic, 1 Metropolitan, 2 Central, 3 Pacific)
        .equ    DORD,     0xFFE370       | its 8 teams, best first
        .equ    STR,      0xFFE380       | text for $11BA4 (length word + text + 0)
        .equ    PLAYMODE, 0xFFD048
        .equ    POSX,     0xFFB028       | text / banner position (tile column, row)
        .equ    POSY,     0xFFB02A
        .equ    ATTR,     0xFFB02C       | tile attribute bits ($6000 = palette 3, the blinking one)
        .equ    ROWS,     8
        .equ    BX,       4              | banner column

        .text
        .globl  _start
_start:
        tst.w   PLAYMODE
        beq.s   1f
        jmp     0x1733E                 | playoffs: the tree as before
1:      cmpi.l  #0x172E4,(%sp)          | only after a game
        bne.s   9f
        lea     BUF,%a0
        move.l  #SRAM_OFF,%d0
        move.l  #NBYTES,%d1
        jsr     SRAM_RD
        cmpi.l  #MAGIC,BUF
        bne.s   9f
        tst.w   BUF+12                  | "New Season" chosen
        bne.s   9f
        tst.w   BUF+4                   | no games yet
        beq.s   9f
        bra.w   show
9:      rts

| ---- the screen: same set-up as the playoff tree ($1733E-$17432), without the bracket and the cup ----
show:   bsr.w   sortteams
        clr.w   PAGE                    | your team's division
        move.w  BUF+6,%d0
        beq.s   2f
        subq.w  #1,%d0
        lea     divs,%a0
        moveq   #0,%d1
        move.b  (%a0,%d0.w),%d1
        move.w  %d1,PAGE
2:      move.l  #0x17692,0xFFB03A       | VBlank handler of the tree screen (horizontal scroll)
        bclr    #1,0xFFBF78
        move.w  #0,0xFFB000
        move.w  #0xBC00,0xFFB002
        move.w  #0xB000,0xFFB00C
        move.w  #6,0xFFB00E
        move.w  #0xC000,0xFFB008
        move.w  #7,0xFFB00A
        move.w  #0xE000,0xFFB004
        move.w  #7,0xFFB006
        moveq   #0,%d0
        jsr     0x11594
        jsr     0x11B92
        .word   6
        .byte   0xFF,0,0,0
        move.l  #0x80,%d0
        moveq   #0x1C,%d1
        move.w  #0x7FF,%d2
        jsr     0x1197E
        jsr     0x11B92
        .word   6
        .byte   0xFD,0,0,0
        moveq   #0x28,%d0
        moveq   #2,%d1
        move.w  #0x7FF,%d2
        jsr     0x1197E
        jsr     0x11F20
        jsr     0x11F04
        movea.l #0xB3648,%a2
        move.w  %d4,0xFFB026
        jsr     0x11738
        jsr     0x11B92
        .word   6
        .byte   0xCE,0,0,0
        movea.l #0x2000,%a0             | background
        movea.l %a0,%a1
        movea.l %a0,%a2
        adda.l  (%a2)+,%a0
        adda.l  (%a2)+,%a1
        clr.w   %d0
        clr.w   %d1
        move.w  (%a1),%d2
        move.w  2(%a1),%d3
        moveq   #3,%d5
        jsr     0x1169A
        movea.l #0xF3098,%a1            | palettes
        lea     8(%a1),%a2
        adda.l  (%a1),%a1
        moveq   #7,%d0
        movea.l #0xFFBD68,%a0
6:      move.l  -0x40(%a0),0x20(%a0)
        move.l  (%a1)+,-0x40(%a0)
        move.l  -0x20(%a0),(%a0)+
        dbra    %d0,6b
        move.w  %d4,0xFFB016
        jsr     0x11738
        move.w  #0x104,0xFFB8B2
        move.w  #0xA0,0xFFB8B0
        move.w  #0x9200,0xC00004         | no window plane (the tree keeps its bottom line in one)
        move.w  #0x160,0xFFD5AC         | horizontal scroll 0 ($FEA0 + $D5AC)
        clr.w   0xFFD5B0
        clr.w   0xFFC2E8                | no sprite upload in VBlank ($17692 DMAs $C2E8 words to the sprite table)
        move.w  %sr,-(%sp)              | and no sprites left from the game: sprite 0 at y 0, end of the list
        move.w  #0x2700,%sr
        move.l  #0x7C000002,0xC00004    | VRAM write $BC00 (sprite table, $B002)
        moveq   #0,%d0
        move.l  %d0,0xC00000
        move.l  %d0,0xC00000
        move.w  (%sp)+,%sr
        bsr.w   divord
        bsr.w   title
        bsr.w   draw
        move.w  #0x18,0xFFBD26
        move.w  #0x79,-(%sp)            | fade in
        jsr     0x11156
loop:   jsr     0x175D8                 | wait for the frame (blinks palette 3)
        jsr     0x11340                 | d1 = new presses, pad 1
        move.w  %d1,-(%sp)
        jsr     0x11358                 | pad 2
        or.w    (%sp)+,%d1
        btst    #7,%d1
        bne.s   done                    | Start
        btst    #3,%d1
        beq.s   1f
        addq.w  #1,PAGE                 | Right: next division
        bra.s   2f
1:      btst    #2,%d1
        beq.s   loop
        addq.w  #3,PAGE                 | Left: previous division
2:      andi.w  #3,PAGE
        bsr.w   divord
        bsr.w   title
        bsr.w   draw
        bra.s   loop
done:   rts                             | back to $172E4: fade out, main menu

| ---- sort the 32 teams: points (2 per win, 1 per OT loss / tie), then goal difference; equal keys keep team order
sortteams:
        lea     BUF+16,%a0
        lea     KEY,%a1
        moveq   #0,%d0
1:      move.w  2(%a0),%d1              | W
        add.w   %d1,%d1
        add.w   6(%a0),%d1              | + OTL
        add.w   12(%a0),%d1             | + T
        lsl.w   #8,%d1
        move.w  8(%a0),%d2              | GF - GA, clamped
        sub.w   10(%a0),%d2
        cmpi.w  #127,%d2
        ble.s   2f
        moveq   #127,%d2
2:      cmpi.w  #-128,%d2
        bge.s   3f
        move.w  #-128,%d2
3:      addi.w  #128,%d2
        add.w   %d2,%d1
        move.w  %d1,(%a1)+
        lea     16(%a0),%a0
        addq.w  #1,%d0
        cmpi.w  #32,%d0
        blt.s   1b
        lea     ORDER,%a2               | selection: take the best remaining each time
        moveq   #31,%d7
4:      lea     KEY,%a1
        moveq   #-1,%d4                 | best key (unsigned compare, taken = -2... use a flag table)
        moveq   #0,%d5
        moveq   #-1,%d6                 | best index
5:      move.w  (%a1,%d5.w),%d1
        cmpi.w  #-1,%d1
        beq.s   6f                      | taken
        cmpi.w  #-1,%d6
        beq.s   7f
        cmp.w   %d4,%d1
        bls.s   6f
7:      move.w  %d1,%d4
        move.w  %d5,%d6
6:      addq.w  #2,%d5
        cmpi.w  #64,%d5
        blt.s   5b
        move.w  #-1,(%a1,%d6.w)
        lsr.w   #1,%d6
        move.b  %d6,(%a2)+
        dbra    %d7,4b
        rts

| DORD = the teams of division PAGE in ORDER's order
divord: movem.l %d0-%d2/%a0-%a2,-(%sp)
        lea     ORDER,%a0
        lea     divs,%a1
        lea     DORD,%a2
        moveq   #0,%d0
1:      moveq   #0,%d1
        move.b  (%a0,%d0.w),%d1
        move.b  (%a1,%d1.w),%d2
        cmp.w   PAGE,%d2
        bne.s   2f
        move.b  %d1,(%a2)+
2:      addq.w  #1,%d0
        cmpi.w  #32,%d0
        blt.s   1b
        movem.l (%sp)+,%d0-%d2/%a0-%a2
        rts

| points of team d0 -> d0
points: move.w  %d0,%d1
        lsl.w   #4,%d1
        lea     BUF+16,%a0
        move.w  2(%a0,%d1.w),%d0
        add.w   %d0,%d0
        add.w   6(%a0,%d1.w),%d0
        add.w   12(%a0,%d1.w),%d0
        rts

| ---- title and column heads
title:  move.w  #9,POSX
        move.w  #1,POSY
        lea     divname,%a0
        move.w  PAGE,%d0
        mulu.w  #22,%d0                 | 21 characters + 0
        adda.w  %d0,%a0
        bsr.w   print
        move.w  #BX+16,POSX
        move.w  #2,POSY
        lea     t_head,%a0
        bsr.w   print
        move.w  #1,POSX
        move.w  #26,POSY
        lea     t_foot,%a0
        bra.w   print

| ---- the division's 8 teams: rank, banner, abbreviation, GP, PTS, gap
draw:   movem.l %d0-%d7/%a0-%a3,-(%sp)
        moveq   #0,%d7                  | screen row
1:      move.w  %d7,%d6                 | place 0..7
        moveq   #0,%d5
        lea     DORD,%a0
        move.b  (%a0,%d6.w),%d5         | team
        move.w  %d7,%d0                 | row on screen: 4 + 3 * i
        mulu.w  #3,%d0
        addq.w  #4,%d0
        move.w  %d0,%d4
        | rank
        move.w  #0,POSX
        move.w  %d4,POSY
        move.w  %d6,%d2
        addq.w  #1,%d2
        lea     STR+2,%a1
        move.b  #0x20,(%a1)+            | "]" (an arrow) in front of your team
        move.w  BUF+6,%d0
        subq.w  #1,%d0
        cmp.w   %d5,%d0
        bne.s   7f
        move.b  #0x5D,-1(%a1)
7:      bsr.w   num2
        move.b  #0x2E,(%a1)+            | "."
        bsr.w   printbuf
        | banner
        jsr     0x11B92
        .word   6
        .byte   0xFF,0,0,0
        clr.w   ATTR
        move.w  BUF+6,%d0
        subq.w  #1,%d0
        cmp.w   %d5,%d0
        bne.s   2f
        move.w  #0x6000,ATTR            | your team: palette 3, blinks
2:      move.w  #BX,POSX
        move.w  %d4,POSY
        subq.w  #1,POSY                 | banner: the row above and this row
        move.w  %d5,%d1
        bsr.w   banner
        clr.w   ATTR
        | GP, PTS, gap
        move.w  #BX+12,POSX
        move.w  %d4,POSY
        lea     STR+2,%a1
        lea     abbr,%a0                | abbreviation (both New York banners read NEW YORK)
        move.w  %d5,%d0
        mulu.w  #3,%d0
        move.b  (%a0,%d0.w),(%a1)+
        move.b  1(%a0,%d0.w),(%a1)+
        move.b  2(%a0,%d0.w),(%a1)+
        move.b  #0x20,(%a1)+
        move.w  %d5,%d0
        lsl.w   #4,%d0
        lea     BUF+16,%a0
        move.w  (%a0,%d0.w),%d2         | GP
        bsr.w   num3s
        move.b  #0x20,(%a1)+
        move.b  #0x20,(%a1)+
        move.w  %d5,%d0
        bsr.w   points
        move.w  %d0,%d3                 | PTS
        move.w  %d3,%d2
        bsr.w   num3s
        move.b  #0x20,(%a1)+
        move.b  #0x20,(%a1)+
        moveq   #0,%d0                  | gap: leader's points - these; leader: + lead over number 2
        lea     DORD,%a0
        tst.w   %d6
        bne.s   3f
        move.b  1(%a0),%d0
        bsr.w   points
        move.w  %d3,%d2
        sub.w   %d0,%d2
        move.b  #0x2B,(%a1)+            | "+"
        bra.s   4f
3:      move.b  (%a0),%d0
        bsr.w   points
        move.w  %d0,%d2
        sub.w   %d3,%d2
        move.b  #0x2D,(%a1)+            | "-"
        tst.w   %d2
        bne.s   4f
        move.b  #0x20,-1(%a1)
4:      bsr.w   num3l
        bsr.w   printbuf
        addq.w  #1,%d7
        cmpi.w  #ROWS,%d7
        blt.w   1b
        movem.l (%sp)+,%d0-%d7/%a0-%a3
        rts

| banner: d1 = team at (POSX, POSY): two rows of the playoff tree's banner map ($17652 without its highlight test)
banner: movem.l %d0-%d7/%a0-%a3,-(%sp)
        add.w   %d1,%d1
        movea.l #0x1DD366,%a1
        adda.l  4(%a1),%a1
        movea.l #0x30A,%a2
        clr.w   %d0
        move.w  (%a1),%d2
        moveq   #2,%d3
        moveq   #2,%d4
        moveq   #0,%d5
        jsr     0x1169A
        movem.l (%sp)+,%d0-%d7/%a0-%a3
        rts

| print: a0 = zero-terminated text at (POSX, POSY)
print:  lea     STR+2,%a1
1:      move.b  (%a0)+,(%a1)+
        bne.s   1b
        subq.l  #1,%a1
| printbuf: text from STR+2 up to a1
printbuf:
        clr.b   (%a1)+
        move.l  %a1,%d0
        btst    #0,%d0
        beq.s   1f
        clr.b   (%a1)+
1:      lea     STR,%a0
        move.l  %a1,%d0
        sub.l   %a0,%d0
        move.w  %d0,(%a0)
        movea.l %a0,%a1
        move.w  #4,0xFFB02E             | always the back plane ($C000): solid dark cells, a redraw replaces the old text
        clr.w   ATTR                    | (the banners go to the front plane, $11B92 code FF sets plane 0)
        jmp     0x11BA4

| num2: d2 (0..99) right-aligned in 2 characters; num3s: 3 characters; num3l: left-aligned, then padded to 3
num2:   andi.l  #0xFFFF,%d2
        divu.w  #10,%d2
        tst.w   %d2
        bne.s   1f
        move.b  #0x20,(%a1)+
        bra.s   2f
1:      addi.b  #0x30,%d2
        move.b  %d2,(%a1)+
2:      swap    %d2
        addi.b  #0x30,%d2
        move.b  %d2,(%a1)+
        rts
num3s:  andi.l  #0xFFFF,%d2
        divu.w  #100,%d2
        move.w  %d2,%d0
        swap    %d2
        tst.w   %d0
        bne.s   1f
        move.b  #0x20,(%a1)+
        bra.w   num2
1:      addi.b  #0x30,%d0
        move.b  %d0,(%a1)+
        andi.l  #0xFFFF,%d2
        divu.w  #10,%d2
        addi.b  #0x30,%d2
        move.b  %d2,(%a1)+
        swap    %d2
        addi.b  #0x30,%d2
        move.b  %d2,(%a1)+
        rts
num3l:  movea.l %a1,%a3
        bsr.s   num3s
        lea     -3(%a1),%a0             | squeeze out the leading blanks
1:      cmpi.b  #0x20,(%a0)
        bne.s   2f
        lea     1(%a0),%a2
        movea.l %a0,%a3
3:      move.b  (%a2)+,(%a3)+
        cmpa.l  %a1,%a2
        blt.s   3b
        move.b  #0x20,-1(%a1)
        bra.s   1b
2:      rts

abbr:   .ascii  "ANABOSBUFCGYCARCHICOLCBJDALDETEDMFLALAKMINMTLNSHNJDNYINYROTTPHIPITSJSSEASTLTBLTORUTAVANVGKWSHWPG"
divs:   .byte   3,0,0,3,1,2,2,1,2,0,3,0,3,2,0,2,1,1,1,0,1,1,3,3,2,0,0,2,3,3,1,2
divname: .asciz "  Atlantic Division  "
        .asciz  "Metropolitan Division"
        .asciz  "  Central Division   "
        .asciz  "  Pacific Division   "
t_head: .ascii  " GP  PTS  Gap"
        .byte   0
t_foot: .ascii  "[ Division ]           Start: continue"
        .byte   0
        .even
