forked from OpenEmu/Bliss-Core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BlissGameCore.mm
1000 lines (828 loc) · 25.9 KB
/
BlissGameCore.mm
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2014, OpenEmu Team
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the OpenEmu Team nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY OpenEmu Team ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL OpenEmu Team BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "BlissGameCore.h"
#if __has_include(<OpenGL/OpenGL.h>)
#import <OpenGL/gl3.h>
#import <OpenGL/gl3ext.h>
#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#else
#import <OpenGLES/ES3/gl.h>
#import <OpenGLES/ES3/glext.h>
#endif
#if __has_include(<UIKit/UIKeyConstants.h>)
#import <UIKit/UIKeyConstants.h>
#endif
#import <PVSupport/PVSupport-Swift.h>
#import "core/Emulator.h"
#import "core/rip/Rip.h"
#import "core/audio/AudioMixer.h"
#import "core/video/VideoBus.h"
#import "drivers/intv/Intellivision.h"
#import "drivers/intv/HandController.h"
#import "drivers/intv/ECSKeyboard.h"
#define INTV_IMAGE_WIDTH (160)
#define INTV_IMAGE_HEIGHT (192)
#define KEYBOARD_OBJECT_COUNT 256
#define AUDIO_SAMPLE_RATE 48000
#define INTY_TO_BITMAP(bits) (1ULL << (uint64_t)(bits))
#define INTY_TEST(bits, flag) ((bits) & (uint64_t)(flag))
#define INTY_ON(bits, flag) ((bits) |= (uint64_t)(flag))
#define INTY_OFF(bits, flag) ((bits) &= ~(uint64_t)(flag))
typedef struct
{
UINT16 keypad;
UINT16 action;
UINT16 disc;
} BlissController;
class BlissInputProducer : public InputProducer
{
public:
BlissInputProducer();
const CHAR* getName() { return "Bliss Input"; }
void poll() {}
INT32 getInputCount() { return 23; }
const CHAR* getInputName(INT32) { return "Bliss Input Name"; }
float getValue(INT32 enumeration);
BOOL isKeyboardDevice() { return keyboardDevice; }
void setKeyboardDevice(BOOL isKeyboardDevice) {
keyboardDevice = isKeyboardDevice;
}
void setPlayer(CHAR playerIndex) {
player = playerIndex;
}
private:
CHAR player;
BOOL keyboardDevice;
};
class BlissAudioMixer : public AudioMixer
{
public:
void init(UINT32 sampleRate);
void release();
void flushAudio();
};
class BlissVideoBus : public VideoBus
{
public:
void init(UINT32 width, UINT32 height);
void release();
void render();
};
@interface PVBlissGameCore () <PVIntellivisionSystemResponderClient>
{
NSLock *_bufferLock;
OERingBuffer *_audioBuffer;
unsigned char *_videoBuffer;
BlissAudioMixer *_audioMixer;
BlissVideoBus *_videoBus;
NSString *_ROMName;
Emulator *currentEmu;
Rip *currentRip;
NSMutableData *_stateData;
}
- (int)blissButtonForIntellivisionButton:(PVIntellivisionButton)button player:(NSUInteger)player;
@end
@implementation PVBlissGameCore
// Global variables because the callbacks need to access them...
static PVBlissGameCore *_currentCore;
static BlissController _controller[2] = {0};
static uint64_t _keyboard = 0;
static uint8_t _keyboardDownCount = 0;
static uint8_t _keyboardShiftCount = 0;
#pragma mark - OpenEmu Core
/*
OpenEmu Core internal functions
*/
- (id)init
{
self = [super init];
if(self != nil)
{
_bufferLock = [[NSLock alloc] init];
_currentCore = self;
_audioMixer = new BlissAudioMixer;
_videoBus = new BlissVideoBus;
_stateData = [NSMutableData dataWithLength:sizeof(IntellivisionState)];
}
return self;
}
- (void)dealloc
{
DLog(@"releasing/deallocating Bliss memory");
delete _videoBus;
_videoBus = NULL;
delete _audioMixer;
_audioMixer = NULL;
_currentCore = NULL;
}
- (void)executeFrame
{
//DLog(@"Executing");
// run the emulation
currentEmu->Run();
// render and display the video
currentEmu->Render();
// flush the audio
currentEmu->FlushAudio();
}
#pragma mark - Bliss Core Helpers
- (BOOL)LoadRip:(const char*)filename
{
char cfgFilename[PATH_MAX] = {0};
NSString *cfgString = [[NSBundle bundleForClass:[self class]] pathForResource:@"knowncarts" ofType:@"cfg" inDirectory:@""];
strncpy(cfgFilename, cfgString.fileSystemRepresentation, sizeof(cfgFilename));
if(!cfgFilename[0])
return FALSE;
if(strlen(filename) < 5)
return FALSE;
const CHAR* extStart = filename + strlen(filename) - 4;
if(strcmpi(extStart, ".intv") == 0 || strcmpi(extStart, ".int") == 0 || strcmpi(extStart, ".bin") == 0)
{
//load the bin file as a Rip
currentRip = Rip::LoadBin(filename, cfgFilename);
if(currentRip == NULL)
return FALSE;
}
else if(strcmpi(extStart, ".a52") == 0)
{
//load the bin file as a Rip
currentRip = Rip::LoadA52(filename);
if(currentRip == NULL)
return FALSE;
CHAR fileSubname[MAX_PATH];
const CHAR* filenameStart = strrchr(filename, '/')+1;
strncpy(fileSubname, filenameStart, strlen(filenameStart)-4);
*(fileSubname+strlen(filenameStart)-4) = NULL;
}
else if(strcmpi(extStart, ".irom") == 0 || strcmpi(extStart, ".rom") == 0)
{
//load the rom file as a Rip
currentRip = Rip::LoadRom(filename);
if(currentRip == NULL)
return FALSE;
CHAR fileSubname[MAX_PATH];
const CHAR* filenameStart = strrchr(filename, '/')+1;
strncpy(fileSubname, filenameStart, strlen(filenameStart)-4);
*(fileSubname+strlen(filenameStart)-4) = NULL;
}
else if(strcmpi(extStart, ".zip") == 0)
{
//load the zip file as a Rip
currentRip = Rip::LoadZip(filename, cfgFilename);
if(currentRip == NULL)
return FALSE;
CHAR fileSubname[MAX_PATH];
const CHAR* filenameStart = strrchr(filename, '/')+1;
strncpy(fileSubname, filenameStart, strlen(filenameStart)-4);
*(fileSubname+strlen(filenameStart)-4) = NULL;
}
else
{
//load the designated Rip
currentRip = Rip::LoadRip(filename);
if(currentRip == NULL)
return FALSE;
}
return TRUE;
}
- (BOOL)loadROMForPeripheral:(Peripheral*)peripheral
{
BOOL didLoadROMs = NO;
NSString *BIOSPath = nil;
UINT16 count = peripheral->GetROMCount();
for(UINT16 i = 0; i < count; i++)
{
ROM* r = peripheral->GetROM(i);
if(r->isLoaded())
{
didLoadROMs = YES;
continue;
}
BIOSPath = [[self BIOSPath] stringByAppendingString:[NSString stringWithFormat:@"/%s", r->getDefaultFileName()]];
if(r->load([BIOSPath fileSystemRepresentation], r->getDefaultFileOffset()))
{
didLoadROMs = YES;
}
else
{
didLoadROMs = NO;
break;
}
}
return didLoadROMs;
}
- (void)ReleasePeripheralInputs:(Peripheral*)periph
{
UINT16 count = periph->GetInputConsumerCount();
for(UINT16 i = 0; i < count; i++)
{
InputConsumer* nextInputConsumer = periph->GetInputConsumer(i);
//iterate through each object on this consumer (buttons, keys, etc.)
int iccount = nextInputConsumer->getInputConsumerObjectCount();
for(int j = 0; j < iccount; j++)
{
InputConsumerObject* nextObject = nextInputConsumer->getInputConsumerObject(j);
if(nextObject)
{
nextObject->clearBindings();
}
}
}
}
- (void)ReleaseEmulatorInputs
{
memset(_controller, 0, sizeof(_controller));
_keyboard = 0;
_keyboardDownCount = 0;
_keyboardShiftCount = 0;
if(!currentEmu)
return;
[self ReleasePeripheralInputs:currentEmu];
UINT32 count = currentEmu->GetPeripheralCount();
for(UINT32 i = 0; i < count; i++)
{
[self ReleasePeripheralInputs:currentEmu->GetPeripheral(i)];
}
}
- (void)InitializePeripheralInputs:(Peripheral*)periph
{
//iterate through all the emulated input consumers in the current emulator.
//these consumers represent the emulated joysticks, keyboards, etc. that were
//originally used to provide input to the emulated system
UINT16 count = periph->GetInputConsumerCount();
for(UINT16 i = 0; i < count; i++)
{
InputConsumer* nextInputConsumer = periph->GetInputConsumer(i);
BOOL isKeyboard = dynamic_cast<ECSKeyboard*>(nextInputConsumer) ? TRUE : FALSE;
//iterate through each object on this consumer (buttons, keys, etc.)
int iccount = nextInputConsumer->getInputConsumerObjectCount();
for(int j = 0; j < iccount; j++)
{
InputConsumerObject* nextObject = nextInputConsumer->getInputConsumerObject(j);
if(nextObject)
{
INT32 _objectids[1] = {nextObject->getDefaultEnum()};
INT32 *objectids = _objectids;
InputProducer** producerList = new InputProducer*[0];
BlissInputProducer *producer = new BlissInputProducer;
producer->setPlayer(i);
producer->setKeyboardDevice(isKeyboard);
producerList[0] = producer;
nextObject->addBinding(producerList, objectids, 1);
delete[] producerList;
}
}
}
}
- (void)InitializeEmulatorInputs
{
[self ReleaseEmulatorInputs];
[self InitializePeripheralInputs:currentEmu];
UINT32 count = currentEmu->GetPeripheralCount();
for(UINT32 i = 0; i < count; i++)
{
[self InitializePeripheralInputs:currentEmu->GetPeripheral(i)];
}
}
#pragma mark - OpenEmu Core
- (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error
{
_ROMName = [path copy];
if(![self LoadRip:path.fileSystemRepresentation])
{
return FALSE;
}
DLog(@"Loaded File");
// find the currentEmulator required to run this RIP
currentEmu = Emulator::GetEmulatorByID(currentRip->GetTargetSystemID());
// load emulator ROMs
if(![self loadROMForPeripheral:currentEmu])
{
return NO;
}
// load peripheral ROMs
INT32 count = currentEmu->GetPeripheralCount();
for(INT32 i = 0; i < count; i++)
{
Peripheral* p = currentEmu->GetPeripheral(i);
PeripheralCompatibility usage = currentRip->GetPeripheralUsage(p->GetShortName());
if(usage == PERIPH_INCOMPATIBLE || usage == PERIPH_COMPATIBLE)
{
currentEmu->UsePeripheral(i, FALSE);
continue;
}
BOOL loaded = [self loadROMForPeripheral:p];
if(loaded)
{
//peripheral loaded, might as well use it.
currentEmu->UsePeripheral(i, TRUE);
}
else if(usage == PERIPH_OPTIONAL)
{
//didn't load, but the peripheral is optional, so just skip it
currentEmu->UsePeripheral(i, FALSE);
}
else
{
//usage == PERIPH_REQUIRED, but it didn't load
return NO;
}
}
[self InitializeEmulatorInputs];
// hook up the audio and video
currentEmu->InitVideo(_videoBus, currentEmu->GetVideoWidth(), currentEmu->GetVideoHeight());
currentEmu->InitAudio(_audioMixer, AUDIO_SAMPLE_RATE);
// put the RIP in the currentEmulator
currentEmu->SetRip(currentRip);
// finally, run everything
currentEmu->Reset();
return YES;
}
- (void)resetEmulation
{
currentEmu->Reset();
}
- (void)stopEmulation
{
if(currentEmu)
{
currentEmu->SetRip(NULL);
currentEmu->ReleaseAudio();
currentEmu->ReleaseVideo();
currentEmu = NULL;
}
if(currentRip)
{
delete currentRip;
currentRip = NULL;
}
[super stopEmulation];
}
- (CGSize)bufferSize
{
return CGSizeMake(INTV_IMAGE_WIDTH, INTV_IMAGE_HEIGHT);
}
- (CGRect)screenRect
{
return CGRectMake(0, 0, INTV_IMAGE_WIDTH, INTV_IMAGE_HEIGHT);
}
- (CGSize)aspectSize
{
return CGSizeMake(INTV_IMAGE_WIDTH * (12.0/7.0), INTV_IMAGE_HEIGHT);
}
- (const void *)getVideoBufferWithHint:(void *)hint
{
if (!hint) {
if (!_videoBuffer) _videoBuffer = new unsigned char[256 * 256 * 4];
hint = _videoBuffer;
}
return _videoBuffer = (uint8_t*)hint;
}
- (const void *)videoBuffer {
return [self getVideoBufferWithHint:nil];
}
- (GLenum)pixelFormat
{
return GL_BGRA;
}
- (GLenum)internalPixelFormat
{
return GL_BGRA;
}
- (GLenum)pixelType
{
return GL_UNSIGNED_INT;
// return GL_UNSIGNED_INT_8_8_8_8_REV;
}
- (NSTimeInterval)frameInterval
{
// http://spatula-city.org/~im14u2c/intv/tech/master.html
// Actual Effective Frame Rate
return 59.92;
}
- (NSUInteger)channelCount
{
return 1;
}
- (double)audioSampleRate
{
return AUDIO_SAMPLE_RATE;
}
- (NSUInteger)audioBitDepth
{
return 16;
}
- (void)saveStateToFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
{
BOOL didSaveStateFile = NO;
didSaveStateFile = currentEmu->SaveStateFile([fileName fileSystemRepresentation]);
block(didSaveStateFile, nil);
}
- (void)loadStateFromFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
{
BOOL didLoadStateFile = NO;
// TODO: is this an emulator bug, a state bug, or a hardware necessity?
// determine if this intellivision cart requires the ECS peripheral.
// if it does, we need to 'warm up' the system with 5 frames before loading
// the state in to the emulator. (only immediately following a reset - why?)
if(currentRip->GetPeripheralUsage("ECS") == PERIPH_REQUIRED)
{
int warmUpECSFrameCount = 5;
while(warmUpECSFrameCount > 0)
{
currentEmu->Run();
warmUpECSFrameCount--;
}
}
didLoadStateFile = currentEmu->LoadStateFile([fileName fileSystemRepresentation]);
block(didLoadStateFile, nil);
}
- (NSData *)serializeStateWithError:(NSError **)outError
{
void *stateBuffer = [_stateData mutableBytes];
NSUInteger stateLength = [_stateData length];
BOOL didSaveStateData = NO;
didSaveStateData = currentEmu->SaveStateBuffer(stateBuffer, stateLength);
if(didSaveStateData)
return _stateData;
if(outError) {
*outError = [NSError errorWithDomain:@"com.provenance.core" code:2 userInfo:@{
NSLocalizedDescriptionKey : @"Save state data could not be written",
NSLocalizedRecoverySuggestionErrorKey : @"The emulator could not write the state data."
}];
}
return nil;
}
- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError
{
const void *stateBuffer = [state bytes];
NSUInteger stateLength = [_stateData length];
BOOL didLoadStateData = NO;
didLoadStateData = currentEmu->LoadStateBuffer(stateBuffer, stateLength);
if(didLoadStateData)
return YES;
if(outError) {
*outError = [NSError errorWithDomain:@"com.provenance.core" code:1 userInfo:@{
NSLocalizedDescriptionKey : @"The save state data could not be read"
}];
}
return NO;
}
#pragma mark -
/*
Bliss callbacks
*/
#pragma mark Bliss Audio Mixer
void BlissAudioMixer::init(UINT32 sampleRate)
{
int sampleInterval = (sampleRate / [_currentCore frameInterval]);
// initialize the sampleBuffer
AudioMixer::init(sampleRate);
_currentCore->_audioBuffer = [_currentCore ringBufferAtIndex:0];
if(_currentCore->_audioBuffer)
{
#warning May not need this
[_currentCore->_audioBuffer setLength:(sizeof(INT16) * sampleInterval * 8)];
}
}
void BlissAudioMixer::release()
{
AudioMixer::release();
}
void BlissAudioMixer::flushAudio()
{
NSUInteger bytesPerSample = sizeof(INT16);
NSUInteger bytesToWrite = sampleCount * bytesPerSample;
[_currentCore->_bufferLock lock];
[_currentCore->_audioBuffer write:this->sampleBuffer maxLength:bytesToWrite];
[_currentCore->_bufferLock unlock];
// updates buffer write position and sample count
AudioMixer::flushAudio();
}
#pragma mark Bliss Video Bus
void BlissVideoBus::init(UINT32 width, UINT32 height)
{
VideoBus::init(width, height);
}
void BlissVideoBus::release()
{
if (_currentCore->_videoBuffer) {
delete[] _currentCore->_videoBuffer;
_currentCore->_videoBuffer = NULL;
}
VideoBus::release();
}
void BlissVideoBus::render()
{
VideoBus::render();
[_currentCore->_bufferLock lock];
memcpy(_currentCore->_videoBuffer, this->pixelBuffer, this->pixelBufferSize);
[_currentCore->_bufferLock unlock];
}
#pragma mark Bliss Input Producer
BlissInputProducer::BlissInputProducer()
: InputProducer((GUID){0})
{
}
float BlissInputProducer::getValue(INT32 enumeration)
{
BOOL isKeyboardDevice = this->isKeyboardDevice();
char player = this->player;
float value = 0.0f;
if(isKeyboardDevice)
{
uint64_t keyflag = INTY_TO_BITMAP(enumeration);
value = INTY_TEST(_keyboard, keyflag) == keyflag ? 1.0f : 0.0f;
}
else
{
if(enumeration >= CONTROLLER_DISC_DOWN && enumeration <= CONTROLLER_DISC_UP_LEFT)
{
value = INTY_TEST(_controller[player].disc, enumeration) == enumeration ? 1.0f : 0.0f;
}
else if(enumeration == CONTROLLER_ACTION_TOP || enumeration == CONTROLLER_ACTION_BOTTOM_LEFT || enumeration == CONTROLLER_ACTION_BOTTOM_RIGHT)
{
value = INTY_TEST(_controller[player].action, enumeration) == enumeration ? 1.0f : 0.0f;
}
else if(enumeration >= CONTROLLER_KEYPAD_THREE && enumeration <= CONTROLLER_KEYPAD_CLEAR)
{
value = INTY_TEST(_controller[player].keypad, enumeration) == enumeration ? 1.0f : 0.0f;
}
}
return value;
}
#pragma mark - PVIntellivisionSystemResponderClient
- (int)blissButtonForIntellivisionButton:(PVIntellivisionButton)button player:(NSUInteger)player;
{
int btn = -1;
static int OEBlissIntellivisionButton[] =
{
CONTROLLER_DISC_UP,
CONTROLLER_DISC_DOWN,
CONTROLLER_DISC_LEFT,
CONTROLLER_DISC_RIGHT,
CONTROLLER_ACTION_TOP,
CONTROLLER_ACTION_BOTTOM_LEFT,
CONTROLLER_ACTION_BOTTOM_RIGHT,
CONTROLLER_KEYPAD_ONE,
CONTROLLER_KEYPAD_TWO,
CONTROLLER_KEYPAD_THREE,
CONTROLLER_KEYPAD_FOUR,
CONTROLLER_KEYPAD_FIVE,
CONTROLLER_KEYPAD_SIX,
CONTROLLER_KEYPAD_SEVEN,
CONTROLLER_KEYPAD_EIGHT,
CONTROLLER_KEYPAD_NINE,
CONTROLLER_KEYPAD_ZERO,
CONTROLLER_KEYPAD_CLEAR,
CONTROLLER_KEYPAD_ENTER
};
if(button < PVIntellivisionButtonCount && button >= PVIntellivisionButtonUp)
{
btn = OEBlissIntellivisionButton[button];
}
return btn;
}
- (oneway void)setIntellivisionButton:(int)btn isDown:(BOOL)down forPlayer:(NSUInteger)player
{
switch(btn)
{
case CONTROLLER_DISC_DOWN:
case CONTROLLER_DISC_RIGHT:
case CONTROLLER_DISC_UP:
case CONTROLLER_DISC_LEFT: {
_controller[player-1].disc = down ?
INTY_ON(_controller[player-1].disc, btn) :
INTY_OFF(_controller[player-1].disc, btn);
// if both horizontal + vertical disc directions are active,
// turn on the wide bit flag for 45-degree angles
if((_controller[player-1].disc & (CONTROLLER_DISC_LEFT|CONTROLLER_DISC_RIGHT))
&& (_controller[player-1].disc & (CONTROLLER_DISC_UP|CONTROLLER_DISC_DOWN)))
{
INTY_ON(_controller[player-1].disc, CONTROLLER_DISC_WIDE);
}
else
{
INTY_OFF(_controller[player-1].disc, CONTROLLER_DISC_WIDE);
}
break;
}
case CONTROLLER_KEYPAD_ONE:
case CONTROLLER_KEYPAD_TWO:
case CONTROLLER_KEYPAD_THREE:
case CONTROLLER_KEYPAD_FOUR:
case CONTROLLER_KEYPAD_FIVE:
case CONTROLLER_KEYPAD_SIX:
case CONTROLLER_KEYPAD_SEVEN:
case CONTROLLER_KEYPAD_EIGHT:
case CONTROLLER_KEYPAD_NINE:
case CONTROLLER_KEYPAD_CLEAR:
case CONTROLLER_KEYPAD_ZERO:
case CONTROLLER_KEYPAD_ENTER:
_controller[player-1].keypad = down ?
INTY_ON(_controller[player-1].keypad, btn) :
INTY_OFF(_controller[player-1].keypad, btn);
break;
case CONTROLLER_ACTION_TOP:
case CONTROLLER_ACTION_BOTTOM_LEFT:
case CONTROLLER_ACTION_BOTTOM_RIGHT:
_controller[player-1].action = down ?
INTY_ON(_controller[player-1].action, btn) :
INTY_OFF(_controller[player-1].action, btn);
break;
default: break;
}
}
- (void)didPushIntellivisionButton:(PVIntellivisionButton)button forPlayer:(NSInteger)player;
{
int btn = [self blissButtonForIntellivisionButton:button player:player];
if(btn > -1)
{
[self setIntellivisionButton:btn isDown:YES forPlayer:player];
}
}
- (void)didReleaseIntellivisionButton:(PVIntellivisionButton)button forPlayer:(NSInteger)player;
{
int btn = [self blissButtonForIntellivisionButton:button player:player];
if(btn > -1)
{
[self setIntellivisionButton:btn isDown:NO forPlayer:player];
}
}
- (int)intellivisionKeyForKeyCode:(unsigned short)keyCode
{
int btn = -1;
#if !TARGET_OS_OSX
switch(keyCode)
{
default: break;
case UIKeyboardHIDUsageKeyboardA: btn = ECS_KEYBOARD_A; break;
case UIKeyboardHIDUsageKeyboardB: btn = ECS_KEYBOARD_B; break;
case UIKeyboardHIDUsageKeyboardC: btn = ECS_KEYBOARD_C; break;
case UIKeyboardHIDUsageKeyboardD: btn = ECS_KEYBOARD_D; break;
case UIKeyboardHIDUsageKeyboardE: btn = ECS_KEYBOARD_E; break;
case UIKeyboardHIDUsageKeyboardF: btn = ECS_KEYBOARD_F; break;
case UIKeyboardHIDUsageKeyboardG: btn = ECS_KEYBOARD_G; break;
case UIKeyboardHIDUsageKeyboardH: btn = ECS_KEYBOARD_H; break;
case UIKeyboardHIDUsageKeyboardI: btn = ECS_KEYBOARD_I; break;
case UIKeyboardHIDUsageKeyboardJ: btn = ECS_KEYBOARD_J; break;
case UIKeyboardHIDUsageKeyboardK: btn = ECS_KEYBOARD_K; break;
case UIKeyboardHIDUsageKeyboardL: btn = ECS_KEYBOARD_L; break;
case UIKeyboardHIDUsageKeyboardM: btn = ECS_KEYBOARD_M; break;
case UIKeyboardHIDUsageKeyboardN: btn = ECS_KEYBOARD_N; break;
case UIKeyboardHIDUsageKeyboardO: btn = ECS_KEYBOARD_O; break;
case UIKeyboardHIDUsageKeyboardP: btn = ECS_KEYBOARD_P; break;
case UIKeyboardHIDUsageKeyboardQ: btn = ECS_KEYBOARD_Q; break;
case UIKeyboardHIDUsageKeyboardR: btn = ECS_KEYBOARD_R; break;
case UIKeyboardHIDUsageKeyboardS: btn = ECS_KEYBOARD_S; break;
case UIKeyboardHIDUsageKeyboardT: btn = ECS_KEYBOARD_T; break;
case UIKeyboardHIDUsageKeyboardU: btn = ECS_KEYBOARD_U; break;
case UIKeyboardHIDUsageKeyboardV: btn = ECS_KEYBOARD_V; break;
case UIKeyboardHIDUsageKeyboardW: btn = ECS_KEYBOARD_W; break;
case UIKeyboardHIDUsageKeyboardX: btn = ECS_KEYBOARD_X; break;
case UIKeyboardHIDUsageKeyboardY: btn = ECS_KEYBOARD_Y; break;
case UIKeyboardHIDUsageKeyboardZ: btn = ECS_KEYBOARD_Z; break;
case UIKeyboardHIDUsageKeyboard1: btn = ECS_KEYBOARD_1; break;
case UIKeyboardHIDUsageKeyboard2: btn = ECS_KEYBOARD_2; break;
case UIKeyboardHIDUsageKeyboard3: btn = ECS_KEYBOARD_3; break;
case UIKeyboardHIDUsageKeyboard4: btn = ECS_KEYBOARD_4; break;
case UIKeyboardHIDUsageKeyboard5: btn = ECS_KEYBOARD_5; break;
case UIKeyboardHIDUsageKeyboard6: btn = ECS_KEYBOARD_6; break;
case UIKeyboardHIDUsageKeyboard7: btn = ECS_KEYBOARD_7; break;
case UIKeyboardHIDUsageKeyboard8: btn = ECS_KEYBOARD_8; break;
case UIKeyboardHIDUsageKeyboard9: btn = ECS_KEYBOARD_9; break;
case UIKeyboardHIDUsageKeyboard0: btn = ECS_KEYBOARD_0; break;
case UIKeyboardHIDUsageKeyboardReturnOrEnter: btn = ECS_KEYBOARD_RETURN; break;
case UIKeyboardHIDUsageKeyboardEscape: btn = ECS_KEYBOARD_ESCAPE; break;
case UIKeyboardHIDUsageKeyboardDeleteOrBackspace: btn = ECS_KEYBOARD_LEFT; break;
case UIKeyboardHIDUsageKeyboardSpacebar: btn = ECS_KEYBOARD_SPACE; break;
case UIKeyboardHIDUsageKeyboardHyphen: btn = ECS_KEYBOARD_6; break; // shifted
case UIKeyboardHIDUsageKeyboardEqualSign: btn = ECS_KEYBOARD_1; break; // shifted
case UIKeyboardHIDUsageKeyboardSemicolon: btn = ECS_KEYBOARD_SEMICOLON; break;
case UIKeyboardHIDUsageKeyboardQuote: btn = ECS_KEYBOARD_RIGHT; break; // shifted
case UIKeyboardHIDUsageKeyboardComma: btn = ECS_KEYBOARD_COMMA; break;
case UIKeyboardHIDUsageKeyboardPeriod: btn = ECS_KEYBOARD_PERIOD; break;
case UIKeyboardHIDUsageKeyboardSlash: btn = ECS_KEYBOARD_7; break; // shifted
case UIKeyboardHIDUsageKeyboardRightArrow: btn = ECS_KEYBOARD_RIGHT; break;
case UIKeyboardHIDUsageKeyboardLeftArrow: btn = ECS_KEYBOARD_LEFT; break;
case UIKeyboardHIDUsageKeyboardDownArrow: btn = ECS_KEYBOARD_DOWN; break;
case UIKeyboardHIDUsageKeyboardUpArrow: btn = ECS_KEYBOARD_UP; break;
case UIKeyboardHIDUsageKeyboardReturn: btn = ECS_KEYBOARD_RETURN; break;
case UIKeyboardHIDUsageKeyboardLeftControl:
case UIKeyboardHIDUsageKeyboardRightControl: btn = ECS_KEYBOARD_CONTROL; break;
case UIKeyboardHIDUsageKeyboardLeftShift:
case UIKeyboardHIDUsageKeyboardRightShift: btn = ECS_KEYBOARD_SHIFT; break;
}
#endif
return btn;
}
- (BOOL)isIntellivisionKeyShiftedForKeycode:(unsigned short)keyCode
{
#if !TARGET_OS_OSX
switch(keyCode)
{
default: break;
case UIKeyboardHIDUsageKeyboardLeftShift:
case UIKeyboardHIDUsageKeyboardRightShift: return YES; break;
case UIKeyboardHIDUsageKeyboardHyphen: return YES; break;
case UIKeyboardHIDUsageKeyboardEqualSign: return YES; break;
case UIKeyboardHIDUsageKeyboardQuote: return YES; break;
case UIKeyboardHIDUsageKeyboardSlash: return YES; break;
}
#endif
return NO;
}
- (void)setIntellivisionKey:(int)key isDown:(BOOL)down isShifted:(BOOL)shifted
{
uint64_t shiftflag = INTY_TO_BITMAP(ECS_KEYBOARD_SHIFT);
// HACK: double re-map
if(_keyboardShiftCount > 0)
{
switch(key)
{
default: break;
case ECS_KEYBOARD_1: key = ECS_KEYBOARD_5; break;
case ECS_KEYBOARD_5: key = ECS_KEYBOARD_LEFT; break;
case ECS_KEYBOARD_6: key = ECS_KEYBOARD_UP; break;
case ECS_KEYBOARD_7: key = ECS_KEYBOARD_DOWN; break;
case ECS_KEYBOARD_RIGHT: key = ECS_KEYBOARD_2; break;
}
}
if(shifted)
{
if(down)
{
if(_keyboardShiftCount == 0)
{
INTY_ON(_keyboard, shiftflag);
}
_keyboardShiftCount++;
}
else
{
_keyboardShiftCount--;
if(_keyboardShiftCount == 0)
{
INTY_OFF(_keyboard, shiftflag);
}
}
//DLog(@"_keyboardShiftCount == %i", _keyboardShiftCount);
}
uint64_t keyflag = INTY_TO_BITMAP(key);
if(down)
{
INTY_ON(_keyboard, keyflag);
_keyboardDownCount++;
}
else
{
INTY_OFF(_keyboard, keyflag);
_keyboardDownCount--;
if(_keyboardDownCount == 0)
{
_keyboard = 0;
}
}
//DLog(@"_keyboardDownCount == %i", _keyboardDownCount);
}
- (oneway void)keyDown:(unsigned short)keyCode
{
int key = [self intellivisionKeyForKeyCode:keyCode];
if(key > -1)
{
BOOL shifted = [self isIntellivisionKeyShiftedForKeycode:keyCode];
[self setIntellivisionKey:key isDown:YES isShifted:shifted];
}
}
- (oneway void)keyUp:(unsigned short)keyCode
{
int key = [self intellivisionKeyForKeyCode:keyCode];
if(key > -1)
{
BOOL shifted = [self isIntellivisionKeyShiftedForKeycode:keyCode];
[self setIntellivisionKey:key isDown:NO isShifted:shifted];
}
}
@end