-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSX2View.m
164 lines (147 loc) · 4.41 KB
/
MSX2View.m
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
/*
* micro MSX2+ - MSX2View for iOS
* -----------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (c) 2023 Yoji Suzuki.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* -----------------------------------------------------------------------------
*/
#import "MSX2View.h"
#import "MSX2Core.h"
#import "MSX2CALayer.h"
#import "sound-ios.h"
@interface MSX2View()
@property (nonatomic) MSX2Core* core;
@property (nonatomic) CADisplayLink* displayLink;
@property (assign) void* sound;
@property (atomic) BOOL started;
@property (atomic) BOOL paused;
@end
@implementation MSX2View
+ (Class)layerClass
{
return [MSX2CALayer class];
}
- (instancetype)init
{
if (self = [super init]) {
[self _init];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder*)coder
{
if (self = [super initWithCoder:coder]) {
[self _init];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self _init];
}
return self;
}
- (void)_init
{
if (!_core) {
_core = [[MSX2Core alloc] init];
_sound = sound_create();
self.opaque = NO;
self.clearsContextBeforeDrawing = NO;
self.multipleTouchEnabled = NO;
self.userInteractionEnabled = NO;
((MSX2CALayer*)self.layer).core = _core;
_displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(_detectVsync)];
_displayLink.preferredFramesPerSecond = 60;
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
}
}
- (void)_detectVsync
{
if (_started && !_paused) {
[_core tickWithPad1:[_delegate didRequirePad1CodeWithView:self]
pad2:0
key:0];
[(MSX2CALayer*)self.layer drawFrameWithCore:_core];
NSData* sound = _core.sound;
sound_enqueue(_sound, sound.bytes, sound.length);
} else {
static unsigned char empty[2940];
sound_enqueue(_sound, empty, sizeof(empty));
}
}
- (void)setupWithCBiosMain:(NSData*)main
logo:(NSData*)logo
sub:(NSData*)sub
rom:(NSData*)rom
romType:(MSX2RomType)romType
select:(NSInteger)select
start:(NSInteger)start
{
[_core setupSecondaryExistWithPage0:NO page1:NO page2:NO page3:YES];
[_core setupWithPrimary:0 secondary:0 index:0 bios:main label:@"MAIN"];
[_core setupWithPrimary:0 secondary:0 index:4 bios:logo label:@"LOGO"];
[_core setupWithPrimary:3 secondary:0 index:0 bios:sub label:@"SUB"];
[_core setupRamWithPrimary:3 secondary:3];
[_core setupSpecialKeyCodeWithSelect:select start:start];
[_core loadRom:rom romType:romType];
[_core reset];
[_delegate didStartWithView:self];
_started = YES;
_paused = NO;
}
- (void)stop
{
if (_started) {
[_delegate didStopWithView:self];
_started = NO;
}
}
- (void)dealloc
{
[self stop];
sound_destroy(_sound);
}
- (nullable NSData*)quickSave
{
return [_core quickSave];
}
- (void)quickLoadWithSaveData:(NSData*)saveData;
{
[_core quickLoadWithSaveData:saveData];
}
- (void)reset
{
[_core reset];
}
- (void)pause
{
_paused = YES;
}
- (void)resume
{
_paused = NO;
}
@end