OSDN Git Service

[VM][General][Qt] Merge upstream 2015-12-31.
[csp-qt/common_source_project-fm7.git] / source / src / vm / prnfile.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2015.12.18-
6
7         [ dummy printer ]
8 */
9
10 #include "prnfile.h"
11
12 void PRNFILE::initialize()
13 {
14         fio = new FILEIO();
15         register_frame_event(this);
16         
17         value = wait_frames = -1;
18         strobe = false;
19 }
20
21 void PRNFILE::release()
22 {
23         close_file();
24         delete fio;
25 }
26
27 void PRNFILE::reset()
28 {
29         close_file();
30         value = -1;
31         strobe = false;
32 }
33
34 void PRNFILE::event_frame()
35 {
36         if(fio->IsOpened() && --wait_frames == 0) {
37                 close_file();
38         }
39 }
40
41 void PRNFILE::write_signal(int id, uint32 data, uint32 mask)
42 {
43         if(id == SIG_PRINTER_DATA) {
44                 value = data;
45         } else if(id == SIG_PRINTER_STROBE) {
46                 bool new_strobe = ((data & mask) != 0);
47                 bool falling = (strobe && !new_strobe);
48                 strobe = new_strobe;
49                 
50                 if(falling && value != -1) {
51                         if(!fio->IsOpened()) {
52                                 open_file();
53                         }
54                         fio->Fputc(value);
55                         // wait 10sec
56 #ifdef SUPPORT_VARIABLE_TIMING
57                         wait_frames = (int)(vm->frame_rate() * 10.0 + 0.5);
58 #else
59                         wait_frames = (int)(FRAMES_PER_SEC * 10.0 + 0.5);
60 #endif
61                 }
62         }
63 }
64
65 uint32 PRNFILE::read_signal(int ch)
66 {
67         if(ch == SIG_PRINTER_BUSY) {
68                 return 0;
69         }
70         return 0;
71 }
72
73 void PRNFILE::open_file()
74 {
75         create_date_file_path(file_path, _MAX_PATH, _T("txt"));
76         fio->Fopen(file_path, FILEIO_WRITE_BINARY);
77 }
78
79 void PRNFILE::close_file()
80 {
81         if(fio->IsOpened()) {
82                 // remove if the file size is less than 2 bytes
83                 bool remove = (fio->Ftell() < 2);
84                 fio->Fclose();
85                 if(remove) {
86                         FILEIO::RemoveFile(file_path);
87                 }
88         }
89 }
90
91 #define STATE_VERSION   1
92
93 void PRNFILE::save_state(FILEIO* state_fio)
94 {
95         state_fio->FputUint32(STATE_VERSION);
96         state_fio->FputInt32(this_device_id);
97         
98         state_fio->FputInt32(value);
99         state_fio->FputBool(strobe);
100 }
101
102 bool PRNFILE::load_state(FILEIO* state_fio)
103 {
104         close_file();
105         
106         if(state_fio->FgetUint32() != STATE_VERSION) {
107                 return false;
108         }
109         if(state_fio->FgetInt32() != this_device_id) {
110                 return false;
111         }
112         value = state_fio->FgetInt32();
113         strobe = state_fio->FgetBool();
114         
115         // post process
116         wait_frames = -1;
117         return true;
118 }
119