Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
boytacean
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
João Magalhães
boytacean
Merge requests
!36
Support for Python
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Support for Python
joamag/python
into
master
Overview
0
Commits
42
Pipelines
37
Changes
19
Merged
João Magalhães
requested to merge
joamag/python
into
master
1 year ago
Overview
0
Commits
42
Pipelines
37
Changes
2
Expand
Related to
#36
1
0
1
1
Merge request reports
Compare
version 23
version 36
e3a981f4
1 year ago
version 35
222ee11c
1 year ago
version 34
1753a825
1 year ago
version 33
b48963b2
1 year ago
version 32
cdd5a7b4
1 year ago
version 31
54848c80
1 year ago
version 30
19891b80
1 year ago
version 29
26ba7a72
1 year ago
version 28
74792ebd
1 year ago
version 27
5938fafc
1 year ago
version 26
00d49db5
1 year ago
version 25
de537427
1 year ago
version 24
473eda85
1 year ago
version 23
7a640442
1 year ago
version 22
4c8802f3
1 year ago
version 21
0d9f9169
1 year ago
version 20
b2c05e27
1 year ago
version 19
0fa59770
1 year ago
version 18
96e51180
1 year ago
version 17
46037bf7
1 year ago
version 16
6127023c
1 year ago
version 15
aff0da24
1 year ago
version 14
5695ff97
1 year ago
version 13
a31add2c
1 year ago
version 12
a96ddf4e
1 year ago
version 11
078472db
1 year ago
version 10
3aebb247
1 year ago
version 9
d57aa9b9
1 year ago
version 8
0da5c767
1 year ago
version 7
17dc77fb
1 year ago
version 6
f95fd229
1 year ago
version 5
1e30737e
1 year ago
version 4
94dd3713
1 year ago
version 3
b9bc1374
1 year ago
version 2
b45f4b9d
1 year ago
version 1
4cf7c444
1 year ago
master (base)
and
version 24
latest version
bb495b1a
42 commits,
1 year ago
version 36
e3a981f4
41 commits,
1 year ago
version 35
222ee11c
40 commits,
1 year ago
version 34
1753a825
39 commits,
1 year ago
version 33
b48963b2
38 commits,
1 year ago
version 32
cdd5a7b4
37 commits,
1 year ago
version 31
54848c80
36 commits,
1 year ago
version 30
19891b80
35 commits,
1 year ago
version 29
26ba7a72
34 commits,
1 year ago
version 28
74792ebd
33 commits,
1 year ago
version 27
5938fafc
32 commits,
1 year ago
version 26
00d49db5
31 commits,
1 year ago
version 25
de537427
30 commits,
1 year ago
version 24
473eda85
29 commits,
1 year ago
version 23
7a640442
28 commits,
1 year ago
version 22
4c8802f3
27 commits,
1 year ago
version 21
0d9f9169
26 commits,
1 year ago
version 20
b2c05e27
25 commits,
1 year ago
version 19
0fa59770
24 commits,
1 year ago
version 18
96e51180
23 commits,
1 year ago
version 17
46037bf7
22 commits,
1 year ago
version 16
6127023c
21 commits,
1 year ago
version 15
aff0da24
20 commits,
1 year ago
version 14
5695ff97
19 commits,
1 year ago
version 13
a31add2c
18 commits,
1 year ago
version 12
a96ddf4e
17 commits,
1 year ago
version 11
078472db
16 commits,
1 year ago
version 10
3aebb247
15 commits,
1 year ago
version 9
d57aa9b9
14 commits,
1 year ago
version 8
0da5c767
12 commits,
1 year ago
version 7
17dc77fb
11 commits,
1 year ago
version 6
f95fd229
10 commits,
1 year ago
version 5
1e30737e
9 commits,
1 year ago
version 4
94dd3713
8 commits,
1 year ago
version 3
b9bc1374
7 commits,
1 year ago
version 2
b45f4b9d
6 commits,
1 year ago
version 1
4cf7c444
5 commits,
1 year ago
Show latest version
2 files
+
10
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
src/python/boytacean/gb.py
0 → 100644
+
253
−
0
Options
from
enum
import
Enum
from
contextlib
import
contextmanager
from
typing
import
Any
,
Iterable
,
Union
,
cast
from
PIL.Image
import
Image
,
frombytes
from
.palettes
import
PALETTES
from
.video
import
VideoCapture
from
.boytacean
import
(
DISPLAY_WIDTH
,
DISPLAY_HEIGHT
,
GameBoy
as
GameBoyRust
,
)
class
GameBoyMode
(
Enum
):
DMG
=
1
CGB
=
2
SGB
=
3
class
GameBoy
:
_frame_index
:
int
=
0
_video
:
Union
[
VideoCapture
,
None
]
=
None
_display
:
Union
[
Any
,
None
]
=
None
def
__init__
(
self
,
mode
=
GameBoyMode
.
DMG
,
ppu_enabled
=
True
,
apu_enabled
=
True
,
dma_enabled
=
True
,
timer_enabled
=
True
,
serial_enabled
=
True
,
load_graphics
=
True
,
load
=
True
,
boot
=
True
,
):
super
().
__init__
()
self
.
_frame_index
=
0
self
.
_video
=
None
self
.
_display
=
None
self
.
_system
=
GameBoyRust
(
mode
.
value
)
self
.
_system
.
set_ppu_enabled
(
ppu_enabled
)
self
.
_system
.
set_apu_enabled
(
apu_enabled
)
self
.
_system
.
set_dma_enabled
(
dma_enabled
)
self
.
_system
.
set_timer_enabled
(
timer_enabled
)
self
.
_system
.
set_serial_enabled
(
serial_enabled
)
if
load_graphics
:
self
.
load_graphics
()
if
load
:
self
.
load
(
boot
=
boot
)
def
_repr_markdown_
(
self
)
->
str
:
return
f
"""
# Boytacean
This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built using the [Rust Programming Language](https://www.rust-lang.org) and is running inside this browser with the help of [WebAssembly](https://webassembly.org).
| Field | Value |
| ------- | ------------------- |
| Version |
{
self
.
version
}
|
| Clock |
{
self
.
clock_freq_s
}
|
"""
def
boot
(
self
):
self
.
_system
.
boot
()
def
load
(
self
,
boot
=
True
):
self
.
_system
.
load
(
boot
)
def
load_rom
(
self
,
filename
:
str
):
self
.
_system
.
load_rom_file
(
filename
)
def
load_rom_data
(
self
,
data
:
bytes
):
self
.
_system
.
load_rom
(
data
)
def
clock
(
self
)
->
int
:
return
self
.
_system
.
clock
()
def
clock_m
(
self
,
count
:
int
)
->
int
:
return
self
.
_system
.
clock_m
(
count
)
def
clocks
(
self
,
count
:
int
)
->
int
:
return
self
.
_system
.
clocks
(
count
)
def
next_frame
(
self
)
->
int
:
cycles
=
self
.
_system
.
next_frame
()
self
.
_frame_index
+=
1
self
.
_on_next_frame
()
return
cycles
def
skip_frames
(
self
,
count
:
int
)
->
int
:
cycles
=
0
for
_
in
range
(
count
):
cycles
+=
self
.
next_frame
()
return
cycles
def
frame_buffer
(
self
)
->
bytes
:
return
self
.
_system
.
frame_buffer
()
def
image
(
self
)
->
Image
:
frame_buffer
=
cast
(
bytes
,
self
.
_system
.
frame_buffer
())
image
=
frombytes
(
"
RGB
"
,
(
DISPLAY_WIDTH
,
DISPLAY_HEIGHT
),
frame_buffer
,
"
raw
"
)
return
image
def
save_image
(
self
,
filename
:
str
,
format
:
str
=
"
png
"
):
image
=
self
.
image
()
image
.
save
(
filename
,
format
=
format
)
def
video
(
self
,
save
=
True
,
display
=
False
,
)
->
Any
:
from
IPython.display
import
display
as
_display
if
self
.
_video
==
None
:
raise
RuntimeError
(
"
Not capturing a video
"
)
video
=
self
.
_video
.
build
(
save
=
save
)
if
display
:
_display
(
video
)
return
video
def
set_palette
(
self
,
name
:
str
):
if
not
name
in
PALETTES
:
raise
ValueError
(
f
"
Unknown palette:
{
name
}
"
)
palette
=
PALETTES
[
name
]
self
.
set_palette_colors
(
palette
)
def
set_palette_colors
(
self
,
colors_hex
:
str
):
self
.
_system
.
set_palette_colors
(
colors_hex
)
def
load_graphics
(
self
):
from
.graphics
import
Display
self
.
_display
=
Display
()
@property
def
ppu_enabled
(
self
)
->
bool
:
return
self
.
_system
.
ppu_enabled
()
def
set_ppu_enabled
(
self
,
value
:
bool
):
self
.
_system
.
set_ppu_enabled
(
value
)
@property
def
apu_enabled
(
self
)
->
bool
:
return
self
.
_system
.
apu_enabled
()
def
set_apu_enabled
(
self
,
value
:
bool
):
self
.
_system
.
set_apu_enabled
(
value
)
@property
def
dma_enabled
(
self
)
->
bool
:
return
self
.
_system
.
dma_enabled
()
def
set_dma_enabled
(
self
,
value
:
bool
):
self
.
_system
.
set_dma_enabled
(
value
)
@property
def
timer_enabled
(
self
)
->
bool
:
return
self
.
_system
.
timer_enabled
()
def
set_timer_enabled
(
self
,
value
:
bool
):
self
.
_system
.
set_timer_enabled
(
value
)
@property
def
serial_enabled
(
self
)
->
bool
:
return
self
.
_system
.
serial_enabled
()
def
set_serial_enabled
(
self
,
value
:
bool
):
self
.
_system
.
set_serial_enabled
(
value
)
@property
def
rom_title
(
self
)
->
str
:
return
self
.
_system
.
rom_title
()
@property
def
version
(
self
)
->
str
:
return
self
.
_system
.
version
()
@property
def
clock_freq_s
(
self
)
->
str
:
return
self
.
_system
.
clock_freq_s
()
@property
def
frame_count
(
self
)
->
int
:
return
self
.
_frame_index
@property
def
palettes
(
self
)
->
Iterable
[
str
]:
return
PALETTES
.
keys
()
@contextmanager
def
video_capture
(
self
,
video_format
=
"
avc1
"
,
video_extension
=
"
mp4
"
,
video_name
=
"
output
"
,
fps
=
5
,
frame_format
=
"
png
"
,
video
=
True
,
save
=
False
,
display
=
True
,
):
self
.
_start_capture
(
video_format
=
video_format
,
video_extension
=
video_extension
,
video_name
=
video_name
,
fps
=
fps
,
frame_format
=
frame_format
,
)
try
:
yield
if
video
:
self
.
video
(
save
=
save
,
display
=
display
)
finally
:
self
.
_stop_capture
()
def
_on_next_frame
(
self
):
if
self
.
_video
!=
None
and
self
.
_video
.
should_capture
(
self
.
_frame_index
):
self
.
_video
.
save_frame
(
self
.
image
(),
self
.
_frame_index
)
self
.
_video
.
compute_next
(
self
.
_frame_index
)
if
self
.
_display
!=
None
and
self
.
_display
.
should_render
(
self
.
_frame_index
):
from
.graphics
import
Display
cast
(
Display
,
self
.
_display
).
render_frame
(
self
.
frame_buffer
())
def
_start_capture
(
self
,
video_format
=
"
avc1
"
,
video_extension
=
"
mp4
"
,
video_name
=
"
output
"
,
fps
=
5
,
frame_format
=
"
png
"
,
):
if
self
.
_video
!=
None
:
raise
RuntimeError
(
"
Already capturing a video
"
)
self
.
_video
=
VideoCapture
(
start_frame
=
self
.
_frame_index
,
video_format
=
video_format
,
video_extension
=
video_extension
,
video_name
=
video_name
,
fps
=
fps
,
frame_format
=
frame_format
,
)
def
_stop_capture
(
self
):
if
self
.
_video
:
self
.
_video
.
cleanup
()
self
.
_video
=
None
Loading