aboutsummaryrefslogtreecommitdiff
path: root/main.fab
blob: a47ec271d728feecadd0cbdaa39638cc4e07a3fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
vars
  U[64] buf         // A buffer that can store the current and next row
  U curr_gen = 0       // Swap these offsets back and forth per generation
  U next_gen = 32
  UU drawing_row_offset = 0  // Use this to track which row we're drawing
  U current_rule = %00011110

fn init_screen()
  // Scroll to attributes
  ppu_reset_addr($2000 + 960)
  // Set attributes for background
  for U i = 0; i < 64; i+= 1
    {PPUDATA}(%11111111)

  //// Init the buffers to 0
  //for i = 0; i < 64; i += 1
  //  buf[i] = 0

  // Init the buffer to zero
  buf = U[64]()
  // Add an initial cell
  buf[16] = 1


fn assemble_code(U left, U middle, U right) U
  U out = %00000000
  out |= left << 2
  out |= middle << 1
  out |= right
  return out

fn calc_next_cell(U left, U middle, U right) U
  U code = assemble_code(left, middle, right)
  ct U mask7 = %10000000
  ct U mask6 = %01000000
  ct U mask5 = %00100000
  ct U mask4 = %00010000
  ct U mask3 = %00001000
  ct U mask2 = %00000100
  ct U mask1 = %00000010
  ct U mask0 = %00000001
  switch code
    case %00000111
      return (current_rule & mask7) >> 7
    case %00000110
      return (current_rule & mask6) >> 6
    case %00000101
      return (current_rule & mask5) >> 5
    case %00000100
      return (current_rule & mask4) >> 4
    case %00000011
      return (current_rule & mask3) >> 3
    case %00000010
      return (current_rule & mask2) >> 2
    case %00000001
      return (current_rule & mask1) >> 1
    case %00000000
      return (current_rule & mask0) >> 0

fn calc_and_advance_gen()
  // Calculate the next row's values
  U left_ind = 0
  U right_ind = 0
  U left_cell = 0
  U mid_cell = 0
  U right_cell = 0
  U next_cell = 0
  for U i = 0; i < 32; i += 1
    // Calculate indices for left and right of cell
    if i == 0
      left_ind = 31
    else
      left_ind = i - 1
    if i == 31
      right_ind = 0
    else
      right_ind = i + 1
    // Get left and right of cell
    left_cell = buf[curr_gen + left_ind]
    mid_cell = buf[curr_gen + i]
    right_cell = buf[curr_gen + right_ind]
    // Calc next cell
    next_cell = calc_next_cell(left_cell, mid_cell, right_cell)
    // Set next cell
    buf[next_gen + i] = next_cell

  // Advance next generation
  if curr_gen == 0
    curr_gen = 32
    next_gen = 0
  else
    curr_gen = 0
    next_gen = 32

  // advance drawing row offset, looping back to top
  drawing_row_offset += 1
  if drawing_row_offset == 30
    drawing_row_offset = 0

fn upload_curr_gen()
  // "scroll" to the row we're at
  UU row_start = $2000
  row_start += (drawing_row_offset * 32)
  ppu_reset_addr(row_start)
  for U i = 0; i < 32; i += 1 //32 columns
    //next_cell = 0 //get_next_gen(i)
    {PPUDATA}(buf[i+curr_gen])


nmi main_nmi()
  // Turn on rendering sprites and bg
  {PPUMASK}(PPUMASK_ON | PPUMASK_NO_CLIP)

  upload_curr_gen()

  ppu_reset_scroll(0, 0)


mode main()
: nmi main_nmi
  // Set the palette:
  palette = example_palette
  ppu_upload_palette()

  // Initialize the screen
  init_screen()

  // Turn the NMI on
  {PPUCTRL}(PPUCTRL_NMI_ON)

  // Loop forever:
  while true
    nmi
    calc_and_advance_gen()

// Define the tileset (commonly called CHR):
chrrom
  file(fmt, "tiles.png")