Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
printer.c
Go to the documentation of this file.
1 /* Minimalistic Enterprise-128 emulator with focus on "exotic" hardware
2  Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
3  Copyright (C)2015-2016,2020-2021 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18 
19 #include "xemu/emutools.h"
20 #include "xemu/emutools_files.h"
21 #include "xemu/emutools_config.h"
22 #include "enterprise128.h"
23 #include "printer.h"
24 #include "dave.h"
25 #include "configdb.h"
26 #include <errno.h>
27 #include <fcntl.h>
28 
29 
30 static int printer_fd = -1;
31 static int fd_to_open = 1;
32 
33 #define BUFFER_SIZE 1024
34 #define COVOX_ACTIVATION_LIMIT 0x100
35 
36 static Uint8 buffer[BUFFER_SIZE];
37 static int buffer_pos;
38 static int strobes_missed = 0;
40 static int printer_is_covox = 0;
41 static int covox_to_warn = 1;
42 static int old_strobe_level = 0;
43 
44 
45 
46 static void write_printer_buffer ( void )
47 {
48  if (buffer_pos && printer_fd >= 0) {
49  if (xemu_safe_write(printer_fd, buffer, buffer_pos) != buffer_pos) {
50  //if (fwrite(buffer, buffer_pos, 1, fp) != 1) {
51  WARNING_WINDOW("Cannot write printer output: %s\nFurther printer I/O has been disabled.", ERRSTR());
52  close(printer_fd);
53  printer_fd = -1;
54  }
55  }
56  buffer_pos = 0;
57 }
58 
59 
60 
61 void printer_close ( void )
62 {
63  if (printer_fd >= 0) {
64  write_printer_buffer();
65  close(printer_fd);
66  DEBUG("Closing printer output file." NL);
67  fd_to_open = 1;
68  printer_fd = -1;
69  }
70 }
71 
72 
73 
75 {
77  if (strobes_missed > COVOX_ACTIVATION_LIMIT) {
78  if (!printer_is_covox) {
79  DEBUG("PRINTER: COVOX: covox mode has been activated on more than %d writes without STROBE" NL, COVOX_ACTIVATION_LIMIT);
80  printer_is_covox = 1;
81  if (covox_to_warn) {
82  covox_to_warn = 0;
83  INFO_WINDOW("COVOX on printer port has been activated. There will be no further messages on this.");
84  }
86  }
87  } else
88  strobes_missed++;
89 }
90 
91 
92 
93 static void send_data_to_printer ( Uint8 data )
94 {
95  //DEBUG("PRINTER GOT DATA: %d" NL, data);
96  if (fd_to_open) {
97  char path[PATH_MAX + 1];
98  //fp = open_emu_file(printfile, "ab", path);
99  printer_fd = xemu_open_file(configdb.printfile, O_WRONLY | O_APPEND | O_CREAT, NULL, path);
100  if (printer_fd < 0)
101  WARNING_WINDOW("Cannot create/append printer output file \"%s\": %s.\nYou can use Xep128 but printer output will not be logged!", path, ERRSTR());
102  else
103  INFO_WINDOW("Printer event, file \"%s\" has been opened for the output.", path);
104  fd_to_open = 0;
105  buffer_pos = 0;
106  }
107  if (printer_fd >= 0) {
108  buffer[buffer_pos++] = data;
109  if (buffer_pos == BUFFER_SIZE)
110  write_printer_buffer();
111  // fprintf(fp, "%c", data);
112  }
113 }
114 
115 
116 
117 void printer_port_check_strobe ( int level )
118 {
119  if (old_strobe_level && !level) {
120  DEBUG("PRINTER: strobe!" NL);
121  //old_strobe_level = level;
122  strobes_missed = 0;
123  if (printer_is_covox) {
124  DEBUG("PRINTER: COVOX: covox mode has been disabled on STROBE, data byte %02Xh is sent for printing" NL, printer_data_byte);
125  printer_is_covox = 0;
127  }
128  send_data_to_printer(printer_data_byte);
129  }/* else
130  DEBUG("PRINTER: NOT strobe: %d -> %d" NL, old_strobe_level, level);*/
131  old_strobe_level = level;
132 }
133 
134 
135 
137 {
138  if (printer_is_covox) {
139  printer_is_covox = 0;
140  strobes_missed = 0;
141  DEBUG("PRINTER: COVOX: covox mode has been disabled on emulator event." NL);
142  }
143 }
configdb_st::printfile
char * printfile
Definition: configdb.h:48
printer_port_check_strobe
void printer_port_check_strobe(int level)
Definition: printer.c:117
emutools.h
xemu_open_file
int xemu_open_file(const char *filename, int mode, int *mode2, char *filepath_back)
Definition: emutools_files.c:469
COVOX_ACTIVATION_LIMIT
#define COVOX_ACTIVATION_LIMIT
Definition: printer.c:34
audio_source
int audio_source
Definition: dave.c:40
WARNING_WINDOW
#define WARNING_WINDOW(...)
Definition: xep128.h:115
INFO_WINDOW
#define INFO_WINDOW(...)
Definition: xep128.h:114
printer_close
void printer_close(void)
Definition: printer.c:61
m65-memcontent-generator.data
data
Definition: m65-memcontent-generator.py:119
Uint8
uint8_t Uint8
Definition: fat32.c:51
emutools_files.h
printer.h
BUFFER_SIZE
#define BUFFER_SIZE
Definition: printer.c:33
NL
#define NL
Definition: fat32.c:37
emutools_config.h
configdb
struct configdb_st configdb
Definition: configdb.c:34
configdb.h
printer_data_byte
Uint8 printer_data_byte
Definition: printer.c:39
dave.h
printer_disable_covox
void printer_disable_covox(void)
Definition: printer.c:136
AUDIO_SOURCE_PRINTER_COVOX
#define AUDIO_SOURCE_PRINTER_COVOX
Definition: dave.h:23
AUDIO_SOURCE_DAVE
#define AUDIO_SOURCE_DAVE
Definition: dave.h:22
ERRSTR
#define ERRSTR()
Definition: enterprise128.h:56
printer_port_set_data
void printer_port_set_data(Uint8 data)
Definition: printer.c:74
enterprise128.h
DEBUG
#define DEBUG(...)
Definition: emutools_basicdefs.h:167
xemu_safe_write
ssize_t xemu_safe_write(int fd, const void *buffer, size_t length)
Definition: emutools_files.c:563