-
Notifications
You must be signed in to change notification settings - Fork 1
/
Entry.pb
352 lines (292 loc) · 10.4 KB
/
Entry.pb
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
; ----------------------------------------------------------------------------------- ;
; ______ ______ _ _ _ ;
; / __ |/ __ | | | | _ (_) ;
; | | //| | | //| | | ____| | _ ___ ____ ____| |_ ___ ____ _ ____ ___ ;
; | |// | | |// | | | / _ | || \ / _ \ / ___) _ | _)/ _ \ / ___) |/ _ ) /___) ;
; | /__| | /__| | |____( ( | | |_) ) |_| | | ( ( | | |_| |_| | | | ( (/ / |___ | ;
; \_____/ \_____/|_______)_||_|____/ \___/|_| \_||_|\___)___/|_| |_|\____) (___/ ;
; ;
; ---------------------------------------------------------------------------------- ;
;----------------------------------
;-- 00SChip8 project --
;----------------------------------
;
; Entry of application
;
; -> Importation of scripts
; -> Boot initialisation code
; -> Render loop
;
;----------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;>> Importation of scripts >>
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Enumeration
#viewport_window
EndEnumeration
IncludeFile("core/variables.pb") ; Defines variables of both emulator and game
IncludeFile("core/memory.pb") ; Handles and emulates memory unit
IncludeFile("core/rom_examinor.pb") ; Contains procedures for retreiving rom information
IncludeFile("core/rom_loader.pb") ; Contains procedures for loading the rom into memory
IncludeFile("core/inputs.pb") ; Handles chip8 specific input controls
IncludeFile("core/force_draw.pb")
IncludeFile("core/intepreter.pb") ; Most important, the intepreter, cpu emulation, memory accessing, game execution
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;>> Boot initialisation code >>
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Initialize all the sprite enviorment for later use
InitSprite()
; Initialize keyboard enviorment for later use
InitKeyboard()
; Initialize sound enviorment for later use
sound_enabled = InitSound()
;result = LoadSound(#beep_sound, "beep.wav") ; Load a beep sound for chip8
result = CatchSound(#beep_sound, ?BEEP)
; VIEWPORT WINDOW
Enumeration
;#viewport_window -> variables.pb
#main_menu
#menu_open_rom_image
#menu_start_game
#menu_reset_game
#menu_speed_1
#menu_speed_2
#menu_speed_3
#menu_speed_4
#menu_speed_5
#menu_speed_6
#menu_speed_7
#menu_speed_8
#menu_speed_normal
#menu_speed_n1
#menu_speed_n2
#menu_speed_n3
#menu_speed_n4
#menu_exit
#menu_website
#menu_about
EndEnumeration
If OpenWindow(#viewport_window, 100,100, screen_width,screen_height, "00SChip8 - 00laboratories 2011-2016", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget )
If CreateMenu(#main_menu, WindowID(#viewport_window))
MenuTitle("&File")
MenuItem(#menu_open_rom_image, "Open ROM image...")
MenuBar()
MenuItem(#menu_exit, "Exit")
MenuTitle("&Game")
MenuItem(#menu_start_game, "Start game")
MenuItem(#menu_reset_game, "Reset game")
MenuBar()
OpenSubMenu("Game speed")
MenuItem(#menu_speed_n4,"-4")
MenuItem(#menu_speed_n3,"-3")
MenuItem(#menu_speed_n2,"-2")
MenuItem(#menu_speed_n1,"-1")
MenuItem(#menu_speed_normal,"normal")
MenuItem(#menu_speed_1,"+1")
MenuItem(#menu_speed_2,"+2")
MenuItem(#menu_speed_3,"+3")
MenuItem(#menu_speed_4,"+4")
MenuItem(#menu_speed_5,"+5")
MenuItem(#menu_speed_6,"+6")
MenuItem(#menu_speed_7,"+7")
MenuItem(#menu_speed_8,"+8")
CloseSubMenu()
MenuTitle("&Help")
MenuItem(#menu_website, "Website...")
MenuItem(#menu_about, "About...")
EndIf
OpenWindowedScreen(WindowID(#viewport_window), 0,0, screen_width,screen_height, 1, 0, 0) ; Rendering screen
EndIf
; Created viewport_window, which is the main window of the emulator
; Set the framerate to 30
SetFrameRate(30)
; SYSTEM SPRITE -> #system_sprite_black256x240
CreateSprite(#system_sprite_blackfill, screen_width, screen_height)
If StartDrawing(SpriteOutput(#system_sprite_blackfill))
Box(0, 0, screen_width, screen_height, RGB(0, 0, 0))
StopDrawing()
EndIf
; SYSTEM SPRITE -> #system_test_cube
CreateSprite(#system_test_cube, 28, 20)
If StartDrawing(SpriteOutput(#system_test_cube))
Box(0, 0, 28, 20, RGB(255, 0, 155))
Box(5, 5, 10, 10, RGB(155, 0, 255))
DrawText(0,0,"123")
StopDrawing()
EndIf
; SYSTEM SPRITE -> #system_test_cube
CreateSprite(#game_pixel_box, 4, 4)
If StartDrawing(SpriteOutput(#game_pixel_box))
Box(0, 0, 4, 4, #White)
StopDrawing()
EndIf
CreateSprite(#game_pixel_box2, 8, 8)
If StartDrawing(SpriteOutput(#game_pixel_box2))
Box(0, 0, 8, 8, #White)
StopDrawing()
EndIf
; SYSTEM SPRITE -> #system_00labs_logo
CatchSprite(#system_00labs_logo, ?IDLE_BACKGROUND)
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;>> Render loop >>
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
TICKS_PER_SECOND = 60
SKIP_TICKS = 0;000 / TICKS_PER_SECOND
Global MAX_FRAMESKIP = 20
next_game_tick = ElapsedMilliseconds()
update_loops=0
; LOAD BIOS GAME
memory_ClearMemory() ; -> core/memory.pb
intepreter_ResetIntepreter() ; -> core/intepreter.pb
rom_loader_LoadBIOS() ; -> core/rom_loader.pb
Global MAX_FRAMESKIP = 20 ; default game speed
emulator_idle = #False ; Not idle anymore, run intepreter again
Repeat
;
; It's very important to process all the events remaining in the queue at each frame
;
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_Gadget
If EventGadget() = 0
End
EndIf
Case #PB_Event_CloseWindow
End
EndSelect
If Event = #PB_Event_Menu
MenuEvent = EventMenu()
Select MenuEvent
Case #menu_start_game
If tf$
emulator_idle = #False ; Not idle anymore, run intepreter again
Else
MessageRequester("00SChip8","No game was loaded!")
EndIf
Case #menu_open_rom_image
tf$=OpenFileRequester("00SChip8, please select a rom to emulate...",GetCurrentDirectory()+"games\","Chip8 ROM|*.*",0)
If tf$
memory_ClearMemory() ; -> core/memory.pb
intepreter_ResetIntepreter() ; -> core/intepreter.pb
rom_loader_LoadRomFile(tf$) ; -> core/rom_loader.pb
Global MAX_FRAMESKIP = 20 ; default game speed
emulator_idle = #False ; Not idle anymore, run intepreter again
EndIf
Case #menu_about
MessageRequester("About 00SChip8","Copyright © 00laboratories 2011-2016"+Chr(13)+Chr(10)+"http://www.00laboratories.com/")
Case #menu_reset_game
If tf$
memory_ClearMemory() ; -> core/memory.pb
intepreter_ResetIntepreter() ; -> core/intepreter.pb
rom_loader_LoadRomFile(tf$) ; -> core/rom_loader.pb
emulator_idle = #False ; Not idle anymore, run intepreter again
Else
MessageRequester("00SChip8","No game was loaded!")
EndIf
Case #menu_exit
End
Case #menu_website
RunProgram("http://00laboratories.com/downloads/emulation/chip-8-emulator", "", "", #PB_Program_Wait)
; -------------------
; --- GAME SPEEDS ---
; -------------------
Case #menu_speed_normal
MAX_FRAMESKIP = 20
Case #menu_speed_n1
MAX_FRAMESKIP = 17
Case #menu_speed_n2
MAX_FRAMESKIP = 14
Case #menu_speed_n3
MAX_FRAMESKIP = 10
Case #menu_speed_n4
MAX_FRAMESKIP = 5
Case #menu_speed_1
MAX_FRAMESKIP = 30
Case #menu_speed_2
MAX_FRAMESKIP = 45
Case #menu_speed_3
MAX_FRAMESKIP = 60
Case #menu_speed_4
MAX_FRAMESKIP = 75
Case #menu_speed_5
MAX_FRAMESKIP = 90
Case #menu_speed_6
MAX_FRAMESKIP = 105
Case #menu_speed_7
MAX_FRAMESKIP = 120
Case #menu_speed_8
MAX_FRAMESKIP = 140
EndSelect
EndIf
Until Event = 0
; -------------------------
; -- FRAME SKIP RESET --
; -------------------------
update_loops = 0
While ElapsedMilliseconds() > next_game_tick And update_loops < MAX_FRAMESKIP
next_game_tick = next_game_tick+SKIP_TICKS
; -----------------------
; -- UPDATE GAME --
; -----------------------
If emulator_idle = #True ; Display idle splashScreen
;If Not direction : direction = 4 : EndIf
;x + direction
;If x > 240 : direction = -2 : EndIf
;If x < 0 : direction = 2 : EndIf
Else
intepreter_NextOpcode()
EndIf
update_loops = update_loops+1
Wend
; -----------------------
; -- BUFFER HANDLING --
; -----------------------
FlipBuffers() ; Flip the buffers
ClearScreen(RGB(0, 0, 0)) ; ClearScreen does not always work on all platforms, to prevent this, draw a black sprite
DisplaySprite(#system_sprite_blackfill, 0, 0) ; black sprite covering buffer
; -----------------------
; -- DRAW GAME --
; -----------------------
If emulator_isbiosrunning = #True ; Display idle splashScreen
DisplaySprite(#system_00labs_logo, 0, 0)
;DisplaySprite(#system_test_cube, x, x)
;Else
EndIf
If screen_width = 256 ; chip 8 mode
For x=0 To screen_pwidth
For y = 0 To screen_pheight
If screen_pixels(x,y)
If screen_pixels(x,y) = #White
DisplaySprite(#game_pixel_box2, (x*8), y*8)
EndIf
EndIf
Next
Next
Else ; super chip mode
For x=0 To screen_pwidth
For y = 0 To screen_pheight
If screen_pixels(x,y)
If screen_pixels(x,y) = #White
DisplaySprite(#game_pixel_box, (x*4), y*4)
EndIf
EndIf
Next
Next
EndIf
;EndIf
ForEver
MessageRequester("This was not supposed to happen", "We appologize for the inconvinience but like.. err, you.. broke out of an infinite loop which is 'impossible'.")
Delay(5000)
DataSection
BIOS:
IncludeBinary "resources/bios.ch8"
IDLE_BACKGROUND:
IncludeBinary "resources/00laboratories.bmp"
BEEP:
IncludeBinary "resources/beep.wav"
EndDataSection
; IDE Options = PureBasic 5.41 LTS (Windows - x86)
; CursorPosition = 325
; FirstLine = 290
; EnableXP