Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 halyavin 1
;
2
;    LIFE.ASM
3
;
4
;    This program displays Conways game of life
5
;
6
;    Compile with FASM v1.49 for DOS;
7
;
8
;    Version 0.1a   20th May   2004
9
;                   Ivan Poddubny
10
;
11
;    Version 0.1    30th March 2004
12
;                   Mike Hibbett
13
;
14
;    This is an experiment to see how small a usefull application can get
15
 
16
include "lang.inc"
17
include "macros.inc"
18
 
19
  use32
20
  org     0x0
21
 
22
  db     'MENUET01'              ; 8 byte id
23
  dd     0x01                    ; header version
24
  dd     START                   ; start of code
25
  dd     I_END                   ; size of image
26
  dd     0xc1000                  ; memory for app
27
  dd     0xc1000                  ; esp
28
  dd     0x0 , 0x0               ; I_Param , I_Icon
29
 
30
;include "DEBUG.INC"
31
macro setcell x,y  { mov     [esi + 512*(y)*3 + (x)*3], al }
32
 
33
START:
34
 
35
    mov     al, 0xFF
36
    mov     esi, I_END
37
 
38
    ; This is the seed pattern.
39
 
40
    ; Life needs a seed pattern, which is 'hardcode' at compile time
41
    ; The grid is 512 wide (x direction) by 512 deep (y direction)
42
    ; setcell take the arguments setcell x,y
43
    ; 0,0 is the top left corner.
44
 
45
;    setcell 200,120
46
;    setcell 201,120
47
;    setcell 200,121
48
;    setcell 199,121
49
;    setcell 200,122
50
 
51
;    setcell 70,120
52
;    setcell 71,120
53
;    setcell 70,121
54
;    setcell 69,121
55
;    setcell 70,122
56
 
57
    mov     eax, 40
58
    mov     ebx, 100101b
59
    int     0x40
60
 
61
    call    draw_window
62
 
63
still:
64
 
65
;    mov  eax, 23                 ; wait here for event
66
;    mov  ebx, 5
67
;    int  0x40
68
    mov  eax, 11
69
    int  0x40
70
 
71
    test eax, eax
72
    je   nokey
73
    cmp  eax,1                  ; redraw request ?
74
    je   red
75
    cmp  eax,3                  ; button in buffer ?
76
    je   button
77
    cmp  eax,6
78
    je   mouse
79
 
80
    jmp  still
81
 
82
 
83
  mouse:
84
    mov  eax, 37
85
    mov  ebx, 2
86
    int  0x40
87
    test eax, eax
88
    jz   still
89
 
90
    mov  eax, 37
91
    mov  ebx, 1
92
    int  0x40
93
    sub  eax, 5*65536+20
94
    mov  ebx, eax
95
    shr  eax, 16
96
    and  ebx, 0xffff
97
 
98
; WRITE COORDINATES
99
;   dpd  eax
100
;   dps  "  "
101
;   dpd  ebx
102
;   dps  <10,13>
103
 
104
    cmp  ax, 0
105
    js   still
106
    cmp  bx, 0
107
    js   still
108
 
109
    shl  ebx, 9
110
    add  ebx, eax
111
    imul ebx, 3
112
    add  ebx, I_END
113
    mov  [ebx], dword 0xFFFFFFFF
114
    jmp  draw
115
 
116
  red:                          ; REDRAW WINDOW
117
    call draw_window
118
    jmp  still
119
 
120
 
121
nokey:
122
    ; cycle life state
123
 
124
mov eax,5
125
mov ebx,5
126
int 0x40
127
 
128
    mov  esi, I_END + 512*3
129
 
130
    mov     al, 0xFF
131
 
132
lifeloop:
133
    mov     ah, 0
134
    cmp     [esi - 3], al
135
    jne     t2
136
    inc     ah
137
t2:
138
    cmp     [esi + 3], al
139
    jne     t3
140
    inc     ah
141
t3:
142
    cmp     [esi - 512*3], al
143
    jne     t4
144
    inc     ah
145
t4:
146
    cmp     [esi + 512*3], al
147
    jne     t5
148
    inc     ah
149
t5:
150
    cmp     [esi - 512*3 - 3], al
151
    jne     t6
152
    inc     ah
153
t6:
154
    cmp     [esi - 512*3 + 3], al
155
    jne     t7
156
    inc     ah
157
t7:
158
    cmp     [esi + 512*3 - 3], al
159
    jne     t8
160
    inc     ah
161
t8:
162
    cmp     [esi + 512*3 + 3], al
163
    jne     tend
164
    inc     ah
165
 
166
tend:
167
    ; If cell is empty but has 3 neigbours, birth
168
    ; If cell is occupied and has 2,3 neigbours, live
169
    ; else die
170
 
171
    cmp     ah, 3
172
    jne     btest
173
    mov     [esi+1], al
174
    jmp     nextcell
175
 
176
btest:
177
    cmp     ah, 2
178
    jne     nextcell
179
    cmp     [esi], al
180
    jne     nextcell
181
    mov     [esi+1], al
182
 
183
nextcell:
184
    add     esi, 3
185
    cmp     esi, I_END + 512*512*3
186
    jne     lifeloop
187
 
188
    ; copy new generation across
189
 
190
 
191
    mov     ecx, 512*512*3
192
    mov     esi, I_END+1
193
    mov     edi, I_END
194
    rep     movsb               ; copy the data across
195
 
196
    mov     ecx, 512*512
197
    mov     esi, I_END
198
nc1:
199
    mov     [esi+2], byte 0
200
    add     esi, 3
201
    loop    nc1
202
draw:
203
    mov     ebx, I_END
204
    mov     ecx, 512*65536+512
205
    mov     edx, 5*65536+22
206
    mov     eax,7
207
    int     0x40
208
 
209
    jmp  still
210
 
211
button:                       ; BUTTON - only close supported
212
    or   eax,-1
213
    int  0x40
214
 
215
 
216
 
217
 
218
;   *********************************************
219
;   *******  WINDOW DEFINITIONS AND DRAW ********
220
;   *********************************************
221
 
222
 
223
draw_window:
224
    mov  eax,12
225
    mov  ebx,1
226
    int  0x40
227
 
228
    mov  eax,0                     ; open window
229
    mov  ebx,50*65536+512+9
230
    mov  ecx,50*65536+512+22+4
231
    mov  edx,0x03000000
232
    int  0x40
233
 
234
    mov  eax,4                     ; WINDOW LABEL
235
    mov  ebx,8*65536+8
236
    mov  ecx,0x10ffffff
237
    mov  edx,header
238
    mov  esi,header.size
239
    int  0x40
240
 
241
    mov     eax,12                    ; function 12:tell os about windowdraw
242
    mov     ebx,2                     ; 2, end of draw
243
    int     0x40
244
 
245
    ret
246
 
247
 
248
 
249
; DATA AREA
250
 
251
header          db  'Life'
252
   .size = $ - header
253
 
254
I_END: