| Season mode, step 1: record the result of every finished game in battery RAM (SRAM).
| Hooked at $7C2E (jsr $FF88E) on the game-over path ($7C16), so it runs once per finished game.
|
| SRAM block at logical offset $1000 (free in the hack), copied to/from the RAM buffer $FFE000:
|   +0  'SESN'          magic
|   +4  word            games recorded
|   +6  word            your team + 1 (0 = no season yet; set by the first Regular Season game a human plays)
|   +8  word            your games played (= index of the next game in the schedule)
|   +10 word            season length (82)
|   +12 word            1 = "New Season" chosen in the menu: the next game clears the old season and starts a new one
|   +14 word            2 = player stats block valid, 3 = player and goalie stats valid (format 3)
| Player stats: SRAM offset $1220, 32 teams x 25 roster slots (1..25; slot 0 is always a goalie) x
| (goals byte, assists byte, shots word) = 3200 bytes, team stride 100.
|   +16 + 16*team       per team (0..31): GP, W, L, OTL, GF, GA, T, OTW   all words (W includes OTW; OTW from 2026-10-22)
| Goalie stats: SRAM offset $E20 (free in the hack: it uses $0-$E1F and $1EF6-$1FFF), 32 teams x 3 goalies (roster
| index 0..2) x 5 bytes: GP, W, then GA (12 bits) and SA (12 bits) packed big-endian = 480 bytes.
| The game's per-slot arrays hold the goalie in slot 0: goals +$B4 = goals against, shots +$E8 = shots against.
| The goalie in net is line 1's G in the team block (+$16A, 1-based roster index).

        .equ    SRAM_OFF, 0x1000
        .equ    BUF,      0xFFE000
        .equ    NBYTES,   16+32*16
        .equ    MAGIC,    0x5345534E
        .equ    SRAM_RD,  0x1A244        | d0 offset, d1 count, a0 dest
        .equ    SRAM_WR,  0x1A1E4        | d0 offset, d1 count, a0 source
        .equ    SRAM_SUM, 0x1A206        | recompute + write the hack's checksum
        .equ    HOME_T,   0xFFC331       | team index (byte)
        .equ    AWAY_T,   0xFFC333
        .equ    HOME_G,   0xFFC6DA       | goals (word)
        .equ    AWAY_G,   0xFFCA3E
        .equ    PERIOD,   0xFFC466       | 0..2 regulation, 3 overtime
        .equ    STATE,    0xFFC2EA       | bit 4 set = intermission highlight / replay, not a real game
        .equ    PLAYMODE, 0xFFD048       | 0 = Regular Season
        .equ    PLAYERS,  0xFFD04A       | 0 Demo, 1 One - Home, 2 One - Visitor, ...
        .equ    SUMBUF,   0xFFE300       | 2 checksum bytes ($1FFE = ~sum, $1FFF = sum)
        .equ    LIST,     0xFFE310       | 32 bytes: teams to pair up
        .equ    PSTAT_OFF,0x1220
        .equ    PSTAT_N,  3200
        .equ    ROWBUF,   0xFFF100       | one team's player row (100 bytes); $FFF000-$FFF005 are the blue-line variables
        .equ    DELTA,    0xFFF0F0       | byte: checksum change of the player rows written so far
        .equ    ROWLEN,   100
        .equ    GOALS,    0xB4           | per-player goals in the game's team stat blocks ($C6CE home, $CA32 away)
        .equ    ASSISTS,  0xCE
        .equ    SHOTS,    0xE8
        .equ    RND,      0x11086        | d0 = n -> d0 = 0..n-1, other registers kept
        .equ    GK_OFF,   0xE20
        .equ    GK_N,     480
        .equ    SHOTSV,   0xFFF0F2       | word: shots of the last dist call
        .equ    GKREC,    0xFFF0F4       | 5 bytes: one goalie record
        .equ    OTF,      0xFFF0FA       | word: simulated game went to overtime
        .equ    GKBUF,    0xFFF200       | 480 bytes, only while clearing (the team screen's RAM, free during a game)
        .equ    LINEG,    0x16A          | team block: line 1, G (1-based roster index)

        .text
        .globl  _start
_start:
        jsr     0xFF88E                 | the instruction we replaced
        movem.l %d0-%d7/%a0-%a6,-(%sp)
        btst    #4,STATE                | highlights from other games use the same game-over path
        bne.w   done
        tst.w   PLAYMODE                | only Regular Season games count
        bne.w   done

        lea     BUF,%a0                 | read the season block
        move.l  #SRAM_OFF,%d0
        move.l  #NBYTES,%d1
        jsr     SRAM_RD
        bsr.w   blocksum                | d7 = 8-bit sum of the block as stored
        move.b  %d7,%d6
        lea     BUF,%a0
        cmpi.l  #MAGIC,(%a0)
        beq.s   1f
        move.w  #NBYTES/2-1,%d2         | first game ever: clear and stamp the block
        movea.l %a0,%a1
2:      clr.w   (%a1)+
        dbra    %d2,2b
        move.l  #MAGIC,(%a0)
1:      tst.w   12(%a0)                 | "New Season" was chosen in the menu: the old season goes now
        beq.s   3f
        move.w  #NBYTES/2-1,%d2
        movea.l %a0,%a1
4:      clr.w   (%a1)+
        dbra    %d2,4b
        move.l  #MAGIC,(%a0)
3:      clr.b   DELTA
        cmpi.w  #3,14(%a0)              | player + goalie blocks in this format yet?
        beq.s   have
        cmpi.w  #2,14(%a0)              | players only (before goalie stats): keep them, start the goalie table
        bne.s   5f
        bsr.w   gkclear
        bra.s   have
5:      bsr.w   clearall                | otherwise clear everything (all 32 rows)
have:
        moveq   #0,%d0
        moveq   #0,%d1
        move.b  HOME_T,%d0
        move.b  AWAY_T,%d1
        cmpi.w  #31,%d0
        bhi.w   done
        cmpi.w  #31,%d1
        bhi.w   done
        move.w  6(%a0),%d2              | your team + 1
        bne.s   haveuser
        move.w  PLAYERS,%d3             | no season yet: start one with the team a human plays
        cmpi.w  #1,%d3
        beq.s   homeuser
        cmpi.w  #2,%d3
        bne.s   record                  | demo / two players: just record the game
        move.w  %d1,%d2
        bra.s   newseason
homeuser:
        move.w  %d0,%d2
newseason:
        addq.w  #1,%d2
        move.w  %d2,6(%a0)
        clr.w   8(%a0)
        move.w  #82,10(%a0)
        bsr.w   clearall
        bra.s   counted
haveuser:
        subq.w  #1,%d2
        cmp.w   %d0,%d2
        beq.s   counted
        cmp.w   %d1,%d2
        bne.w   done                    | not your game: not part of the season
counted:
        addq.w  #1,8(%a0)
record:
        addq.w  #1,4(%a0)
        lsl.w   #4,%d0
        lsl.w   #4,%d1
        lea     16(%a0,%d0.w),%a1       | home row
        lea     16(%a0,%d1.w),%a2       | away row
        move.w  HOME_G,%d2
        move.w  AWAY_G,%d3
        addq.w  #1,(%a1)                | GP
        addq.w  #1,(%a2)
        add.w   %d2,8(%a1)              | GF / GA
        add.w   %d3,10(%a1)
        add.w   %d3,8(%a2)
        add.w   %d2,10(%a2)
        cmp.w   %d3,%d2
        beq.s   tie
        bhi.s   homewin
        addq.w  #1,2(%a2)               | away win
        movea.l %a1,%a3                 | loser = home
        movea.l %a2,%a4                 | winner = away
        bra.s   loser
homewin:
        addq.w  #1,2(%a1)
        movea.l %a2,%a3
        movea.l %a1,%a4
loser:
        cmpi.w  #3,PERIOD
        bne.s   reg
        addq.w  #1,6(%a3)               | overtime loss
        addq.w  #1,14(%a4)              | overtime win
        bra.s   save
reg:    addq.w  #1,4(%a3)               | regulation loss
        bra.s   save
tie:    addq.w  #1,12(%a1)
        addq.w  #1,12(%a2)
save:
        bsr.w   addreal                 | goals and assists from this game
        bsr.w   gkreal                  | and both goalies
        bsr.w   simleague               | the other 30 teams play one game each
        lea     BUF,%a0
        move.l  #SRAM_OFF,%d0
        move.l  #NBYTES,%d1
        jsr     SRAM_WR
        | update the hack's checksum without touching RAM $FF0000 (its own routine reloads all 8 KB there,
        | which corrupts the replay data and crashed the post-game screens)
        bsr.w   blocksum                | d7 = new sum of the block
        add.b   DELTA,%d7               | + the change of the player rows (already written)
        lea     SUMBUF,%a0
        move.l  #0x1FFE,%d0
        moveq   #2,%d1
        jsr     SRAM_RD
        lea     SUMBUF,%a0
        move.b  1(%a0),%d0              | old total
        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
done:
        movem.l (%sp)+,%d0-%d7/%a0-%a6
        rts

| d7.b = 8-bit sum of the NBYTES bytes in BUF
blocksum:
        lea     BUF,%a1
        moveq   #0,%d7
        move.w  #NBYTES-1,%d2
bs:     add.b   (%a1)+,%d7
        dbra    %d2,bs
        rts

| ---- simulate one game for every team except the two that just played (d0/d1 still hold their row offsets*16?)
| Uses HOME_T/AWAY_T again. Goals per team: 8 tries, each scores if rnd(256) < 60 + strength/4.
simleague:
        movem.l %d0-%d7/%a0-%a4,-(%sp)
        moveq   #0,%d4
        moveq   #0,%d5
        move.b  HOME_T,%d4
        move.b  AWAY_T,%d5
        lea     LIST,%a2
        moveq   #0,%d0
        moveq   #0,%d6                  | count
sl1:    cmp.b   %d4,%d0
        beq.s   sl2
        cmp.b   %d5,%d0
        beq.s   sl2
        move.b  %d0,(%a2,%d6.w)
        addq.w  #1,%d6
sl2:    addq.w  #1,%d0
        cmpi.w  #32,%d0
        blt.s   sl1
        | shuffle (Fisher-Yates)
        move.w  %d6,%d7
        subq.w  #1,%d7
sl3:    tst.w   %d7
        ble.s   sl4
        move.w  %d7,%d0
        addq.w  #1,%d0
        jsr     RND                     | 0..i
        move.b  (%a2,%d7.w),%d1
        move.b  (%a2,%d0.w),(%a2,%d7.w)
        move.b  %d1,(%a2,%d0.w)
        subq.w  #1,%d7
        bra.s   sl3
sl4:    moveq   #0,%d7                  | pair index
sl5:    move.w  %d7,%d0
        addq.w  #1,%d0
        cmp.w   %d6,%d0
        bge.w   sldone
        moveq   #0,%d4
        moveq   #0,%d5
        move.b  (%a2,%d7.w),%d4         | team A
        move.b  1(%a2,%d7.w),%d5        | team B
        move.w  %d4,%d0
        bsr.w   goals
        move.w  %d0,%d2                 | goals A
        move.w  %d5,%d0
        bsr.w   goals
        move.w  %d0,%d3                 | goals B
        lea     BUF+16,%a0
        move.w  %d4,%d0
        lsl.w   #4,%d0
        lea     (%a0,%d0.w),%a1
        move.w  %d5,%d0
        lsl.w   #4,%d0
        lea     (%a0,%d0.w),%a3
        moveq   #0,%d1                  | overtime flag
        cmp.w   %d3,%d2
        bne.s   sl6
        moveq   #1,%d1                  | tie: one goal in overtime, coin flip
        moveq   #2,%d0
        jsr     RND
        tst.w   %d0
        beq.s   sl7
        addq.w  #1,%d2
        bra.s   sl6
sl7:    addq.w  #1,%d3
sl6:    move.w  %d1,OTF                 | (d1 is the goals argument of dist below)
        move.w  %d4,%d0                 | scorers, assists and shots for both teams (final score, overtime included)
        move.w  %d2,%d1
        bsr.w   dist
        move.w  SHOTSV,-(%sp)           | A's shots
        move.w  %d5,%d0
        move.w  %d3,%d1
        bsr.w   dist
        movem.l %d0-%d7,-(%sp)          | goalies: A faces B's goals and shots, B faces A's
        moveq   #0,%d6
        cmp.w   %d3,%d2
        bls.s   1f
        moveq   #1,%d6
1:      move.w  %d4,%d0
        move.w  %d3,%d1
        move.w  SHOTSV,%d2
        move.w  %d6,%d3
        bsr.w   gksim
        movem.l (%sp),%d0-%d7
        moveq   #0,%d6
        cmp.w   %d2,%d3
        bls.s   2f
        moveq   #1,%d6
2:      move.w  %d5,%d0
        move.w  %d2,%d1
        move.w  32(%sp),%d2
        move.w  %d6,%d3
        bsr.w   gksim
        movem.l (%sp)+,%d0-%d7
        addq.l  #2,%sp
        addq.w  #1,(%a1)
        addq.w  #1,(%a3)
        add.w   %d2,8(%a1)
        add.w   %d3,10(%a1)
        add.w   %d3,8(%a3)
        add.w   %d2,10(%a3)
        cmp.w   %d3,%d2
        bhi.s   sl8
        addq.w  #1,2(%a3)               | B won
        movea.l %a1,%a4                 | a4 = loser, a0 = winner
        movea.l %a3,%a0
        bra.s   sl9
sl8:    addq.w  #1,2(%a1)               | A won
        movea.l %a3,%a4
        movea.l %a1,%a0
sl9:    tst.w   OTF
        beq.s   sl10
        addq.w  #1,14(%a0)              | overtime win
        addq.w  #1,6(%a4)
        bra.s   sl11
sl10:   addq.w  #1,4(%a4)
sl11:   addq.w  #2,%d7
        bra.w   sl5
sldone: movem.l (%sp)+,%d0-%d7/%a0-%a4
        rts

| d0 = team -> d0 = goals
goals:  movem.l %d1-%d3/%a0,-(%sp)
        lea     strength,%a0
        moveq   #0,%d2
        move.b  (%a0,%d0.w),%d2
        lsr.w   #2,%d2
        addi.w  #60,%d2                 | chance out of 256
        moveq   #0,%d1                  | goals
        moveq   #7,%d3
g1:     move.w  #256,%d0
        jsr     RND
        cmp.w   %d2,%d0
        bge.s   g2
        addq.w  #1,%d1
g2:     dbra    %d3,g1
        move.w  %d1,%d0
        movem.l (%sp)+,%d1-%d3/%a0
        rts

| ---- player stats: SRAM rows of 100 bytes per team, slots 1..25 x (goals, assists, shots word)
| rowload: d0 = team -> ROWBUF, DELTA -= sum of the stored bytes
rowload:
        movem.l %d0-%d2/%a0-%a1,-(%sp)
        mulu.w  #ROWLEN,%d0
        addi.l  #PSTAT_OFF,%d0
        lea     ROWBUF,%a0
        moveq   #ROWLEN,%d1
        jsr     SRAM_RD
        bsr.w   rowsum
        sub.b   %d2,DELTA
        movem.l (%sp)+,%d0-%d2/%a0-%a1
        rts
| rowsave: d0 = team <- ROWBUF, DELTA += sum of the new bytes
rowsave:
        movem.l %d0-%d2/%a0-%a1,-(%sp)
        bsr.w   rowsum
        add.b   %d2,DELTA
        mulu.w  #ROWLEN,%d0
        addi.l  #PSTAT_OFF,%d0
        lea     ROWBUF,%a0
        moveq   #ROWLEN,%d1
        jsr     SRAM_WR
        movem.l (%sp)+,%d0-%d2/%a0-%a1
        rts
rowsum: lea     ROWBUF,%a1              | d2.b = sum of ROWBUF
        moveq   #0,%d2
        moveq   #ROWLEN-1,%d1
1:      add.b   (%a1)+,%d2
        dbra    %d1,1b
        rts
| clearall: zero all 32 rows, mark the block valid (BUF+14 = 2)
clearall:
        movem.l %d0-%d1/%a1,-(%sp)
        moveq   #0,%d0
1:      bsr.s   rowload
        lea     ROWBUF,%a1
        moveq   #ROWLEN/2-1,%d1
2:      clr.w   (%a1)+
        dbra    %d1,2b
        bsr.s   rowsave
        addq.w  #1,%d0
        cmpi.w  #32,%d0
        blt.s   1b
        bsr.w   gkclear                 | and the goalie table (sets BUF+14 = 3)
        movem.l (%sp)+,%d0-%d1/%a1
        rts

| ---- goalie stats (SRAM $E20, see the top) ----
| gkclear: zero the goalie table, BUF+14 = 3
gkclear:
        movem.l %d0-%d2/%a0-%a1,-(%sp)
        lea     GKBUF,%a0
        move.l  #GK_OFF,%d0
        move.l  #GK_N,%d1
        jsr     SRAM_RD
        lea     GKBUF,%a1
        move.w  #GK_N-1,%d1
1:      move.b  (%a1),%d0
        sub.b   %d0,DELTA
        clr.b   (%a1)+
        dbra    %d1,1b
        lea     GKBUF,%a0
        move.l  #GK_OFF,%d0
        move.l  #GK_N,%d1
        jsr     SRAM_WR
        move.w  #3,BUF+14
        movem.l (%sp)+,%d0-%d2/%a0-%a1
        rts

| gkput: d0 = team, d1 = goalie (roster index 0..2), d2 = goals against, d3 = shots against, d4 = 1 if his team won
gkput:  movem.l %d0-%d7/%a0-%a1,-(%sp)
        cmpi.w  #2,%d1
        bhi.w   9f
        move.w  %d2,%d6                 | GA
        mulu.w  #3,%d0
        add.w   %d1,%d0
        mulu.w  #5,%d0
        addi.l  #GK_OFF,%d0
        move.l  %d0,%d7                 | SRAM offset of the record
        lea     GKREC,%a0
        moveq   #5,%d1
        jsr     SRAM_RD
        bsr.w   gksum
        sub.b   %d2,DELTA
        lea     GKREC,%a0
        addq.b  #1,(%a0)                | GP
        add.b   %d4,1(%a0)              | W
        moveq   #0,%d0                  | d0 = GA (12 bits)
        move.b  2(%a0),%d0
        lsl.w   #4,%d0
        moveq   #0,%d1
        move.b  3(%a0),%d1
        move.w  %d1,%d2
        lsr.w   #4,%d1
        or.w    %d1,%d0
        andi.w  #15,%d2                 | d2 = SA (12 bits)
        lsl.w   #8,%d2
        move.b  4(%a0),%d2
        add.w   %d6,%d0
        cmpi.w  #0xFFF,%d0
        bls.s   1f
        move.w  #0xFFF,%d0
1:      add.w   %d3,%d2
        cmpi.w  #0xFFF,%d2
        bls.s   2f
        move.w  #0xFFF,%d2
2:      move.w  %d0,%d1
        lsr.w   #4,%d1
        move.b  %d1,2(%a0)
        move.w  %d0,%d1
        lsl.w   #4,%d1
        move.w  %d2,%d5
        lsr.w   #8,%d5
        or.w    %d5,%d1
        move.b  %d1,3(%a0)
        move.b  %d2,4(%a0)
        bsr.w   gksum
        add.b   %d2,DELTA
        move.l  %d7,%d0
        lea     GKREC,%a0
        moveq   #5,%d1
        jsr     SRAM_WR
9:      movem.l (%sp)+,%d0-%d7/%a0-%a1
        rts
gksum:  lea     GKREC,%a1               | d2.b = sum of the 5 record bytes
        moveq   #0,%d2
        moveq   #4,%d1
1:      add.b   (%a1)+,%d2
        dbra    %d1,1b
        rts

| gksim: d0 = team, d1 = GA, d2 = SA, d3 = won -> the starter plays 4 games in 5, the backup the rest (gk.inc)
gksim:  movem.l %d0-%d4/%a0,-(%sp)
        move.w  %d3,%d4
        move.w  %d2,%d3
        move.w  %d1,%d2
        lea     gktab,%a0
        move.w  %d0,%d1
        lsl.w   #2,%d1
        adda.w  %d1,%a0
        move.w  %d0,%d1
        moveq   #100,%d0
        jsr     RND
        exg     %d0,%d1                 | d0 = team, d1 = roll
        cmpi.w  #80,%d1
        bcs.s   1f
        moveq   #0,%d1
        move.b  1(%a0),%d1              | backup
        bra.s   2f
1:      moveq   #0,%d1
        move.b  (%a0),%d1               | starter
2:      bsr.w   gkput
        movem.l (%sp)+,%d0-%d4/%a0
        rts

| gkreal: the goalies of the game just played (goalie in net = line 1 G; slot 0 = goals / shots against)
gkreal: movem.l %d0-%d4/%a1,-(%sp)
        movea.l #0xFFFFC6CE,%a1
        moveq   #0,%d0
        move.b  HOME_T,%d0
        moveq   #0,%d4
        move.w  HOME_G,%d1
        cmp.w   AWAY_G,%d1
        bls.s   1f
        moveq   #1,%d4
1:      bsr.s   3f
        movea.l #0xFFFFCA32,%a1
        moveq   #0,%d0
        move.b  AWAY_T,%d0
        moveq   #0,%d4
        move.w  AWAY_G,%d1
        cmp.w   HOME_G,%d1
        bls.s   2f
        moveq   #1,%d4
2:      bsr.s   3f
        movem.l (%sp)+,%d0-%d4/%a1
        rts
3:      moveq   #0,%d1
        move.b  LINEG(%a1),%d1
        subq.w  #1,%d1                  | 1-based -> roster index (0 = no goalie: -1, skipped by gkput)
        moveq   #0,%d2
        move.b  GOALS(%a1),%d2
        moveq   #0,%d3
        move.b  SHOTS(%a1),%d3
        bra.w   gkput

| add the goals/assists/shots of the game just played (home block $C6CE, away $CA32; slot 0 = team total, skipped)
addreal:
        movem.l %d0-%d3/%a0-%a2,-(%sp)
        moveq   #0,%d0
        move.b  HOME_T,%d0
        movea.l #0xFFFFC6CE,%a1
        bsr.s   1f
        moveq   #0,%d0
        move.b  AWAY_T,%d0
        movea.l #0xFFFFCA32,%a1
        bsr.s   1f
        movem.l (%sp)+,%d0-%d3/%a0-%a2
        rts
1:      bsr.w   rowload
        lea     ROWBUF,%a2
        lea     GOALS(%a1),%a1
        moveq   #1,%d1
2:      moveq   #0,%d2
        move.b  (%a1,%d1.w),%d2
        add.b   %d2,(%a2)
        move.b  ASSISTS-GOALS(%a1,%d1.w),%d2
        add.b   %d2,1(%a2)
        move.b  SHOTS-GOALS(%a1,%d1.w),%d2
        add.w   %d2,2(%a2)
        addq.l  #4,%a2
        addq.w  #1,%d1
        cmpi.w  #26,%d1
        blt.s   2b
        bra.w   rowsave

| dist: d0 = team, d1 = goals -> scorer + up to two assists per goal, then the team's other shots
| (22-37 shots in all, each goal is a shot by its scorer). Weights from pweights.inc.
dist:   movem.l %d0-%d7/%a0-%a3,-(%sp)
        move.w  %d0,%d6
        move.w  %d1,%d7
        bsr.w   rowload
        lea     ROWBUF-4,%a3            | slot k is at ROWBUF + (k-1)*4
        moveq   #22,%d0                 | shots this game
        move.w  %d0,-(%sp)
        moveq   #16,%d0
        jsr     RND
        add.w   %d0,(%sp)
        move.w  (%sp),SHOTSV
        bra.s   9f
1:      lea     pw_goal,%a0
        bsr.w   pick                    | d0 = scorer slot
        move.w  %d0,%d4
        lsl.w   #2,%d0
        addq.b  #1,(%a3,%d0.w)          | goal
        addq.w  #1,2(%a3,%d0.w)         | and a shot
        subq.w  #1,(%sp)
        moveq   #-1,%d5
        moveq   #1,%d3                  | up to two assists
2:      move.w  #100,%d0
        jsr     RND
        cmpi.w  #82,%d0
        bge.s   8f
        lea     pw_ast,%a0
        bsr.w   pick
        cmp.w   %d4,%d0
        beq.s   7f
        cmp.w   %d5,%d0
        beq.s   7f
        move.w  %d0,%d5
        lsl.w   #2,%d0
        addq.b  #1,1(%a3,%d0.w)         | assist
7:      dbra    %d3,2b
8:
9:      dbra    %d7,1b
        move.w  (%sp)+,%d7              | the rest of the shots
        bra.s   11f
10:     lea     pw_goal,%a0
        bsr.w   pick
        lsl.w   #2,%d0
        addq.w  #1,2(%a3,%d0.w)
11:     subq.w  #1,%d7
        bpl.s   10b
        move.w  %d6,%d0
        bsr.w   rowsave
        movem.l (%sp)+,%d0-%d7/%a0-%a3
        rts
| pick: a0 = weight table base, d6 = team -> d0 = slot (0..25), weighted
pick:   movem.l %d1-%d3/%a0,-(%sp)
        move.w  %d6,%d1
        mulu.w  #28,%d1                 | per team: word total + 26 byte weights
        adda.w  %d1,%a0
        move.w  (%a0)+,%d0              | total
        jsr     RND                     | 0..total-1
        moveq   #0,%d1
1:      moveq   #0,%d2
        move.b  (%a0,%d1.w),%d2
        sub.w   %d2,%d0
        bmi.s   2f
        addq.w  #1,%d1
        cmpi.w  #25,%d1
        blt.s   1b
2:      move.w  %d1,%d0
        movem.l (%sp)+,%d1-%d3/%a0
        rts

        .include "strength.inc"
        .include "pweights.inc"
        .include "gk.inc"
