stop music on error screen
[d2d-psx.git] / src / error.c
blobd806398396f0bd4325f554396f9c2d2962064d99
1 /*
2 * Copyright (C) Prikol Software 1996-1997
3 * Copyright (C) Aleksey Volynskov 1996-1997
4 * Copyright (C) <ARembo@gmail.com> 2011
5 * Copyright (C) fgsfds 2019
7 * This file is part of the Doom2D PSX project.
9 * Doom2D PSX is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Doom2D PSX is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/> or
20 * write to the Free Software Foundation, Inc.,
21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "glob.h"
25 #include <stdio.h>
26 #include <string.h>
27 // #include <conio.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include "keyb.h"
32 #include "sound.h"
33 #include "vga.h"
34 #include "memory.h"
35 #include "files.h"
36 #include "error.h"
37 #include "memcard.h"
39 #include <ctype.h>
40 #include <sys/types.h>
41 #include <libetc.h>
42 #include <libgte.h>
43 #include <libgpu.h>
44 #include <libspu.h>
46 // length of morse stuff in vblanks
47 #define DIT 2
48 #define DAH 6
49 #define LSP DAH
50 #define WSP (7 * DIT)
52 #define SINADDR 0x1100
53 #define SINCH SPU_16CH
55 // ERR_ messages are now defined in error.h
57 extern SpuVoiceAttr chanattr;
59 static const u_char morse[][5] = {
60 // digits first
61 { DAH, DAH, DAH, DAH, DAH, }, // 0
62 { DIT, DAH, DAH, DAH, DAH, }, // 1
63 { DIT, DIT, DAH, DAH, DAH, }, // 2
64 { DIT, DIT, DIT, DAH, DAH, }, // 3
65 { DIT, DIT, DIT, DIT, DAH, }, // 4
66 { DIT, DIT, DIT, DIT, DIT, }, // 5
67 { DAH, DIT, DIT, DIT, DIT, }, // 6
68 { DAH, DAH, DIT, DIT, DIT, }, // 7
69 { DAH, DAH, DAH, DIT, DIT, }, // 8
70 { DAH, DAH, DAH, DAH, DIT, }, // 9
71 // letters
72 { DIT, DAH, }, // A
73 { DAH, DIT, DIT, DIT, }, // B
74 { DAH, DIT, DAH, DIT, }, // C
75 { DAH, DIT, DIT, }, // D
76 { DIT, }, // E
77 { DIT, DIT, DAH, DIT, }, // F
78 { DAH, DAH, DIT, }, // G
79 { DIT, DIT, DIT, DIT, }, // H
80 { DIT, DIT, }, // I
81 { DIT, DAH, DAH, DAH, }, // J
82 { DAH, DIT, DAH, }, // K
83 { DIT, DAH, DIT, DIT, }, // L
84 { DAH, DAH, }, // M
85 { DAH, DIT, }, // N
86 { DAH, DAH, DAH, }, // O
87 { DIT, DAH, DAH, DIT, }, // P
88 { DAH, DAH, DIT, DAH, }, // Q
89 { DIT, DAH, DIT, }, // R
90 { DIT, DIT, DIT, }, // S
91 { DAH, }, // T
92 { DIT, DIT, DAH, }, // U
93 { DIT, DIT, DIT, DAH, }, // V
94 { DIT, DAH, DAH, }, // W
95 { DAH, DIT, DIT, DAH, }, // X
96 { DAH, DIT, DAH, DAH, }, // Y
97 { DAH, DAH, DIT, DIT, }, // Z
100 static const u_char sinewave[] = {
101 0x26, 0x06, 0x30, 0x10, 0x01, 0x11, 0x01, 0x21,
102 0x10, 0x21, 0x20, 0x11, 0x21, 0x11, 0x12, 0x21,
103 0x27, 0x02, 0x24, 0x33, 0x34, 0x43, 0x33, 0x44,
104 0x33, 0x35, 0x53, 0x43, 0x34, 0x35, 0x44, 0x44,
105 0x27, 0x02, 0x34, 0x35, 0x35, 0x44, 0x44, 0x34,
106 0x35, 0x44, 0x34, 0x44, 0x43, 0x43, 0x34, 0x43,
107 0x27, 0x02, 0x33, 0x24, 0x34, 0x33, 0x42, 0x32,
108 0x23, 0x23, 0x32, 0x22, 0x13, 0x22, 0x12, 0x22,
109 0x28, 0x02, 0x32, 0x32, 0x22, 0x22, 0x21, 0x20,
110 0x01, 0x01, 0x00, 0x00, 0x0f, 0x0e, 0xef, 0xfe,
111 0x27, 0x02, 0xff, 0xef, 0xff, 0xef, 0xfe, 0xfd,
112 0xee, 0xee, 0xfd, 0xdd, 0xde, 0xde, 0xdd, 0xce,
113 0x27, 0x02, 0xce, 0xdd, 0xdd, 0xdc, 0xcd, 0xdc,
114 0xcd, 0xdc, 0xcc, 0xdc, 0xcc, 0xcc, 0xbd, 0xcd,
115 0x27, 0x02, 0xdb, 0xcc, 0xdc, 0xcb, 0xcd, 0xdb,
116 0xdc, 0xdb, 0xcc, 0xcd, 0xdc, 0xcc, 0xdd, 0xdc,
117 0x27, 0x02, 0xcd, 0xdd, 0xdd, 0xec, 0xdd, 0xed,
118 0xdd, 0xee, 0xdd, 0xdf, 0xdf, 0xfe, 0xee, 0xfe,
119 0x28, 0x03, 0xce, 0xde, 0xee, 0xfe, 0xee, 0xe0,
120 0xf0, 0x0f, 0x00, 0x10, 0x00, 0x02, 0x12, 0x12,
123 char err_msg[256];
125 // shits out an error message and locks up
126 void ERR_panic(char *msg) {
127 RECT clearRect = { 0, 0, SCRW, SCRH*2 };
128 DISPENV disp;
129 DRAWENV draw;
130 int i, t;
132 SpuSetKey(SPU_OFF, SPU_ALLCH);
134 S_stopmusic();
135 ResetCallback();
136 ResetGraph(0);
138 SetDefDispEnv(&disp, 0, 0, SCRW, SCRH);
139 disp.isinter = disp.isrgb24 = 0;
140 disp.screen.y = SCRY;
141 disp.screen.h = SCRH;
142 SetDefDrawEnv(&draw, 0, 0, SCRW, SCRH);
144 PutDispEnv(&disp);
145 PutDrawEnv(&draw);
146 ClearImage(&clearRect, 80, 0, 0);
147 DrawSync(0);
149 SetDispMask(1);
151 printf("FATAL ERROR: %s\n", msg);
153 // prepare sound
155 SpuSetTransferMode(SpuTransByDMA);
156 SpuSetTransferStartAddr(SINADDR);
157 SpuWrite((u_char *)sinewave, sizeof(sinewave));
158 SpuIsTransferCompleted(SPU_TRANSFER_WAIT);
160 // prepare channel
162 chanattr.voice = SINCH;
163 chanattr.mask = SPU_VOICE_VOLL | SPU_VOICE_VOLR | SPU_VOICE_WDSA |
164 SPU_VOICE_PITCH;
165 chanattr.addr = SINADDR;
166 chanattr.pitch = 0x3FFF;
167 chanattr.volume.left = chanattr.volume.right = 0x2FFF;
168 SpuSetVoiceAttr(&chanattr);
170 VSync(0);
172 // do the beeps
173 // have to do this because regular VSync(n) tends to time out and die
174 #define VSYNCN(x) t = x; while (t-- > 0) VSync(0)
175 while (1) {
176 VSYNCN(WSP * 6);
178 for (i = 0; i < 256 && msg[i]; ++i) {
179 int c = toupper(msg[i]);
180 int j;
182 if (c >= '0' && c <= '9')
183 c -= '0';
184 else if (c >= 'A' && c <= 'Z')
185 c = c - 'A' + 10;
186 else if (c == '-')
187 c = 'M' - 'A' + 10; // `M`inus
188 else {
189 VSYNCN(WSP);
190 continue;
193 for (j = 0; j < 5 && morse[c][j]; ++j) {
194 SpuSetKey(SPU_ON, SINCH);
195 VSYNCN(morse[c][j]);
196 SpuSetKey(SPU_OFF, SINCH);
197 VSYNCN(DAH);
200 VSYNCN(LSP);
205 void ERR_close_all(void) {
206 S_done();
207 S_donemusic();
208 K_done();
209 mc_done();
210 V_done();
211 M_shutdown();
214 void ERR_quit(void) {
215 void * p;
217 // V_done();
218 // if(!(p=malloc(4000)))
219 puts("ERR_quit()\n");
220 // else {
221 // F_loadres(F_getresid("ENDOOM"),p,0,4000);
222 // memcpy((void*)0xB8000,p,4000);free(p);gotoxy(1,24);
223 // }
224 ERR_close_all();
225 exit(0);