MusicKit  0.0.0
dsp.h
00001 #ifndef __MK_dsp_H___
00002 #define __MK_dsp_H___
00003 /*
00004         dsp.h - master include file for the DSP library (-ldsp -lsys_s)
00005 
00006         Copyright 1988-1992, NeXT Inc.  All rights reserved.
00007  
00008 */
00009 
00010 #ifndef DSP_H
00011 #define DSP_H
00012 
00013 #include "MKDSPDefines.h"
00014 
00015 #define DSP_SYS_VER_C 65
00016 #define DSP_SYS_REV_C 41
00017 
00018 /* Default start address in DSP internal program memory (just after DEGMON) */
00019 #define DSP_PLI_USR_C 0x96
00020 
00021 #define DSPMK_I_NTICK 0x10
00022 
00023 #define DSP_EXT_RAM_SIZE_MIN 8192 /* Minimum external RAM size in 3byte wds */
00024 #define DSP_CLOCK_RATE (25.0E6) /* DSP clock frequency */
00025 #define DSP_CLOCK_PERIOD (40.0E-9) /* Cycle time in seconds. */
00026 
00027 #import "dsp_types.h"
00028 
00029 /****************************** Masks **************************************/
00030 
00031 /* Basic DSP data types */
00032 #define DSP_WORD_MASK           0x00FFFFFF /* low-order 24 bits */
00033 #define DSP_SOUND_MASK          0x0000FFFF /* low-order 16 bits */
00034 #define DSP_ADDRESS_MASK        0x0000FFFF /* low-order 16 bits */
00035 
00036 
00037 /* Bits in the DSP Interrupt Control Register (ICR) */
00038 #define DSP_ICR                 0 /* ICR address in host interface */
00039 #define DSP_ICR_RREQ            0x00000001 /* enable host int. on data->host */
00040 #define DSP_ICR_TREQ            0x00000002 /* enable host int. on data<-host */
00041 #define DSP_ICR_UNUSED          0x00000004
00042 #define DSP_ICR_HF0             0x00000008
00043 #define DSP_ICR_HF1             0x00000010
00044 #define DSP_ICR_HM0             0x00000020
00045 #define DSP_ICR_HM1             0x00000040
00046 #define DSP_ICR_INIT            0x00000080
00047 
00048 #define DSP_ICR_REGS_MASK       0xFF000000
00049 #define DSP_ICR_RREQ_REGS_MASK  0x01000000
00050 #define DSP_ICR_TREQ_REGS_MASK  0x02000000
00051 #define DSP_ICR_HF0_REGS_MASK   0x08000000
00052 #define DSP_ICR_HF1_REGS_MASK   0x10000000
00053 #define DSP_ICR_HM0_REGS_MASK   0x20000000
00054 #define DSP_ICR_HM1_REGS_MASK   0x40000000
00055 #define DSP_ICR_INIT_REGS_MASK  0x80000000
00056 
00057 
00058 /* Bits in the DSP Command Vector Register */
00059 #define DSP_CVR                 1          /* address in host interface */
00060 #define DSP_CVR_HV_MASK         0x0000001F /* low-order  5 bits of CVR */
00061 #define DSP_CVR_HC_MASK         0x00000080 /* HC bit of DSP CVR */
00062 
00063 #define DSP_CVR_REGS_MASK       0x00FF0000 /* Regs mask for CVR */
00064 #define DSP_CVR_HV_REGS_MASK    0x001F0000 /* low-order  5 bits of CVR */
00065 #define DSP_CVR_HC_REGS_MASK    0x00800000 /* Regs mask for HC bit of CVR */
00066 
00067 
00068 /* Bits in the DSP Interrupt Status Register */
00069 #define DSP_ISR              2  /* address in host interface */
00070 #define DSP_ISR_RXDF            0x00000001
00071 #define DSP_ISR_TXDE            0x00000002
00072 #define DSP_ISR_TRDY            0x00000004
00073 #define DSP_ISR_HF2             0x00000008
00074 #define DSP_BUSY                0x00000008 /* "DSP Busy"=HF2 */
00075 #define DSP_ISR_HF3             0x00000010
00076 #define DSP_ISR_UNUSED          0x00000020
00077 #define DSP_ISR_DMA             0x00000040
00078 #define DSP_ISR_HREQ            0x00000080
00079 
00080 #define DSP_ISR_REGS_MASK       0x0000FF00
00081 #define DSP_ISR_RXDF_REGS_MASK  0x00000100
00082 #define DSP_ISR_TXDE_REGS_MASK  0x00000200
00083 #define DSP_ISR_TRDY_REGS_MASK  0x00000400
00084 #define DSP_ISR_HF2_REGS_MASK   0x00000800
00085 #define DSP_BUSY_REGS_MASK      0x00000800
00086 #define DSP_ISR_HF3_REGS_MASK   0x00001000
00087 #define DSP_ISR_DMA_REGS_MASK   0x00004000
00088 #define DSP_ISR_HREQ_REGS_MASK  0x00008000
00089 
00090 /* DSP Interrupt Vector Register */
00091 #define DSP_IVR              3  /* address in host interface */
00092 
00093 #define DSP_UNUSED           4  /* address in host interface */
00094 
00095 /* DSP Receive-Byte Registers */
00096 #define DSP_RXH              5  /* address in host interface */
00097 #define DSP_RXM              6  /* address in host interface */
00098 #define DSP_RXL              7  /* address in host interface */
00099 
00100 /* DSP Transmit-Byte Registers */
00101 #define DSP_TXH              5  /* address in host interface */
00102 #define DSP_TXM              6  /* address in host interface */
00103 #define DSP_TXL              7  /* address in host interface */
00104 
00105 /* Interesting places in DSP memory */
00106 #define DSP_MULAW_SPACE 1       /* memory space code for mulaw table in DSP */
00107 #define DSP_MULAW_TABLE 256     /* address of mulaw table in X onchip memory */
00108 #define DSP_MULAW_LENGTH 128    /* length  of mulaw table */
00109 
00110 #define DSP_ALAW_SPACE 1        /* memory space code for Mu-law table in DSP */
00111 #define DSP_ALAW_TABLE 384      /* address of A-law table */
00112 #define DSP_ALAW_LENGTH 128     /* length  of A-law table */
00113 
00114 #define DSP_SINE_SPACE 2        /* memory space code for sine ROM in DSP */
00115 #define DSP_SINE_TABLE 256      /* address of sine table in Y onchip memory */
00116 #define DSP_SINE_LENGTH 256     /* length  of sine table */
00117 
00118 /* Host commands (cf. DSP56000/1 DSP User's Manual) */
00119 /* To issue a host command, set CVR to (DSP_CVR_HC_MASK|<cmd>&DSP_CVR_HV_MASK) */
00120 
00121 #define DSP_HC_RESET                     (0x0)     /* RESET host command */
00122 #define DSP_HC_TRACE                     (0x4>>1)  /* TRACE host command */
00123 #define DSP_HC_SWI                       (0x6>>1)  /* SWI host command */
00124 #define DSP_HC_SOFTWARE_INTERRUPT        (0x6>>1)  /* SWI host command */
00125 #define DSP_HC_ABORT                     (0x8>>1)  /* DEBUG_HALT */
00126 #define DSP_HC_HOST_RD                   (0x24>>1) /* DMA read done */
00127 #define DSP_HC_HOST_R_DONE               (0x24>>1) /* DMA read done */
00128 #define DSP_HC_EXECUTE_HOST_MESSAGE      (0x26>>1) /* Used for host messages */
00129 #define DSP_HC_XHM                       (0x26>>1) /* abbreviated version */
00130 #define DSP_HC_DMAWT                     (0x28>>1) /* Terminate DMA write */
00131 #define DSP_HC_DMA_HOST_W_DONE           (0x28>>1) /* Terminate DMA write */
00132 #define DSP_HC_HOST_W_DONE               (0x28>>1) /* Terminate DMA write */
00133 #define DSP_HC_KERNEL_ACK                (0x2A>>1) /* Kernel acknowledge */
00134 #define DSP_HC_SYS_CALL                  (0x2C>>1) /* cf <nextdev/snd_dsp.h> */
00135 
00136 #import "dsp_messages.h"
00137 
00138 #define DSP_MESSAGE_OPCODE(x) (((x)>>16)&0xFF)
00139 #define DSP_MESSAGE_SIGNED_DATUM(x) \
00140         ((int)((x)&0x8000?0xFFFF0000|((x)&0xFFFF):((x)&0xFFFF)))
00141 #define DSP_MESSAGE_UNSIGNED_DATUM(x) ((x)&0xFFFF)
00142 #define DSP_MESSAGE_ADDRESS(x) DSP_MESSAGE_UNSIGNED_DATUM(x)
00143 #define DSP_ERROR_OPCODE_INDEX(x) ((x) & 0x7F)   /* Strip MSB on 8-bit field */
00144 #define DSP_IS_ERROR_MESSAGE(x) ((x) & 0x800000) /* MSB on in 24 bits */
00145 #define DSP_IS_ERROR_OPCODE(x) ((x) & 0x80) /* MSB on in 8 bits */
00146 
00147 #define DSP_START_ADDRESS DSP_PLI_USR /* cf. dsp_messages.h */
00148 
00149 /* Be sure to enclose in {} when followed by an else */
00150 #define DSP_UNTIL_ERROR(x) if ((DSPErrorNo=(x))) \
00151   return(_DSPError(DSPErrorNo,"Aborting"))
00152 
00153 #define NOT_YET 1
00154 /* Make alloca() safe */
00155 
00156 /**** Include files ****/
00157 #include "dsp_structs.h"                /* DSP struct declarations */
00158 #include "dsp_errno.h"          /* Error codes for DSP C functions */
00159 #include "libdsp.h"             /* Function prototypes for libdsp functions */
00160 
00161 #if defined(WIN32) && !defined(__MINGW32__)
00162 // Stephen Brandon: MINGW32 has winsock.h imported anyway and it conflicts
00163 // LMS it turns out -ObjC++ barfs including winsock.h
00164 // so these are taken from winsock.h
00165 typedef unsigned int u_int;
00166 /*
00167  * Structure used in select() call, taken from the BSD file sys/time.h.
00168  */
00169 struct timeval {
00170         long    tv_sec;         /* seconds */
00171         long    tv_usec;        /* and microseconds */
00172 };
00173 #else // WIN32
00174  #include <unistd.h>
00175 #endif
00176 
00177 #include <sys/types.h> // LMS replacement
00178 #include <stdio.h>
00179 #include <math.h>
00180 
00181 #ifndef GNUSTEP
00182 # import <mach/mach.h>
00183 #endif
00184 
00185 #define DSP_MAX_HM (DSP_NB_HMS-2) /* Leave room in HMS for begin/end marks */
00186 
00187 #define DSPMK_UNTIMED NULL      /* Denotes untimed, not tick-synchronized */
00188 
00189 /*** GLOBAL VARIABLES ***/      /* defined in DSPGlobals.c */
00190 MKDSP_API int DSPErrorNo;
00191 MKDSP_API DSPTimeStamp DSPMKTimeStamp0; /* Tick-synchronized, untimed */
00192 
00193 /* Numerical conversion */
00194 #define DSP_TWO_TO_24   ((double)16777216.0)
00195 #define DSP_TWO_TO_M_24 ((double)5.960464477539063e-08)
00196 #define DSP_TWO_TO_23   ((double)8388608.0)
00197 #define DSP_TWO_TO_M_23 ((double)1.192092895507813e-7)
00198 #define DSP_TWO_TO_48   ((double)281474976710656.0)
00199 #define DSP_TWO_TO_M_48 ((double)3.552713678800501e-15)
00200 #define DSP_TWO_TO_15   ((double)32768.0)
00201 #define DSP_TWO_TO_M_15 ((double)3.0517578125e-05)
00202 
00203 #define DSP_INT_TO_FLOAT(x) ((((float)(x))*((float)DSP_TWO_TO_M_23)))
00204 #define DSP_SHORT_TO_FLOAT(x) ((((float)(x))*((float)DSP_TWO_TO_M_15)))
00205 #define DSP_FIX24_TO_FLOAT(x) ((((float)(x))*((float)DSP_TWO_TO_M_23)))
00206 #define DSP_INT_TO_DOUBLE(x) ((((double)(x))*(DSP_TWO_TO_M_23)))
00207 #define DSP_SHORT_TO_DOUBLE(x) ((((double)(x))*((float)DSP_TWO_TO_M_15)))
00208 #define DSP_FIX48_TO_DOUBLE(x) ((double)((x)->high24)*(DSP_TWO_TO_24) + ((double) (x)->low24))
00209 #define DSP_FLOAT_TO_INT(x) ((int)(((double)(x))*((double)DSP_TWO_TO_23)+0.5))
00210 #define DSP_FLOAT_TO_SHORT(x) ((int)(((double)(x))*((double)DSP_TWO_TO_15)+0.5))
00211 #define DSP_DOUBLE_TO_INT(x) ((int)(((double)(x))*(DSP_TWO_TO_23)+0.5))
00212 #define DSP_DOUBLE_TO_SHORT(x) ((int)(((double)(x))*((double)DSP_TWO_TO_15)+0.5))
00213 
00214 /* Max positive DSP float = (1-1/2^23) */
00215 #define DSP_F_MAXPOS ((double)(1.0-DSP_TWO_TO_M_23))
00216 #define DSP_ONE DSP_F_MAXPOS
00217 
00218 /* Max negative DSP float = -2^23 */
00219 #define DSP_F_MAXNEG (float) -1.0 
00220 
00221 #define DSP_FIX24_CLIP(_x) (((int)_x) & 0xFFFFFF)
00222 
00223 #define DSPMK_LOW_SAMPLING_RATE 22050.0
00224 #define DSPMK_HIGH_SAMPLING_RATE 44100.0
00225 
00226 #ifndef REMEMBER
00227 #define REMEMBER(x) /* x */
00228 #endif
00229 
00230 #define DSPMK_NTICK DSPMK_I_NTICK
00231 
00232 /*** FILE NAMES ***/
00233 /*** These filenames must stay in synch with those in $DSP/Makefile.config ***/
00234 
00235 #define DSP_SYSTEM_DIRECTORY @"/usr/local/lib/dsp/"
00236 #define DSP_FALLBACK_SYSTEM_DIRECTORY @"/usr/lib/dsp/"
00237 #define DSP_BIN_DIRECTORY "/usr/local/bin/"
00238 #define DSP_AP_BIN_DIRECTORY "/apbin/"
00239 #define DSP_INSTALL_ROOT "/usr/local/lib/dsp/"
00240 #define DSP_ERRORS_FILE "/tmp/dsperrors"
00241 #define DSP_WHO_FILE "/tmp/dsp.who"
00242 
00243 MKDSP_API const char *DSPGetDSPDirectory();     /* as above or $DSP if $DSP set */
00244 MKDSP_API char *DSPGetSystemDirectory();        /* /u/l/l/monitor|$DSP/monitor */
00245 MKDSP_API char *DSPGetImgDirectory();   /* /u/l/l/dsp/img or $DSP/img */
00246 MKDSP_API char *DSPGetAPDirectory();    /* /u/l/l/dsp/imgap or $DSP/imgap */
00247 MKDSP_API char *DSPGetMusicDirectory(); /* DSP_MUSIC_DIRECTORY */
00248 MKDSP_API char *DSPGetLocalBinDirectory(); /* /usr/bin or $DSP/bin */
00249 
00250 /* 
00251    Convert Y-space address in DSP "XY memory partition" 
00252    (where X and Y memories are each 4K long addressed from 0xA000)
00253    into an equivalent address in the "overlaid memory partition"
00254    (where memory addresses are irrespective of space).
00255    *** NOTE: This macro depends on there being 8K words of DSP static RAM ***
00256    Behaves like a function returning int.
00257 */
00258 #define DSPMapPMemY(ya) ((int) ((ya) & 0x7FFF))
00259 
00260 /* 
00261    Convert X-space address in DSP "XY memory partition" 
00262    into an equivalent address in the "overlaid memory partition"
00263    *** NOTE: This macro depends on there being 8K words of DSP static RAM ***
00264    Behaves like a function returning int.
00265 */
00266 #define DSPMapPMemX(xa) ((int) (((xa)|0x1000) & 0x7FFF))
00267 
00268 /* 
00269    File names below are ALL assumed to exist in the directory returned by
00270    DSPGetSystemDirectory(): 
00271 */
00272 
00273 /* Default AP and MK monitors installed with the system */
00274 
00275 /* dsp0 */
00276 
00277 /* All these should be NSConstantStrings but currently they are included in C source - LMS */
00278 
00279 /* The following are used by _DSPRelocate.c for backward 2.0 compatibility */
00280 #define DSP_MUSIC_SYSTEM_MEM "/mkmon8k.mem" 
00281 #define DSP_AP_SYSTEM_MEM "/apmon8k.mem"
00282 
00283 /* The following are used by DSPObject.c DSPBoot.c */
00284 #define DSP_MUSIC_SYSTEM_BINARY_0 "/mkmon_A_8k.dsp" 
00285 #define DSP_MUSIC_SYSTEM_0 "/mkmon_A_8k.lod"
00286 
00287 #define DSP_AP_SYSTEM_BINARY_0 "/apmon_8k.dsp"
00288 #define DSP_AP_SYSTEM_0 "/apmon_8k.lod"
00289 
00290 /* NeXT 32K word DSP memory expansion SIMM */
00291 #define DSP_32K_MUSIC_SYSTEM_BINARY_0 @"/mkmon_A_32k.dsp"
00292 #define DSP_32K_MUSIC_SYSTEM_0 @"/mkmon_A_32k.lod"
00293 
00294 /* NeXT 32K word DSP memory expansion SIMM */
00295 #define DSP_32K_AP_SYSTEM_BINARY_0 @"/apmon_32k.dsp"
00296 #define DSP_32K_AP_SYSTEM_0 @"/apmon_32k.lod"
00297 
00298 /* UCSF 64K x 3 word DSP memory expansion SIMM */
00299 #define DSP_192K_MUSIC_SYSTEM_BINARY_0 @"/mkmon_A_192k.dsp"
00300 #define DSP_192K_MUSIC_SYSTEM_0 @"/mkmon_A_192k.lod"
00301 
00302 /* Ariel PC56D 16K x 2 + 32K (P) word DSP memory */
00303 #define DSP_64K_SPLIT_MUSIC_SYSTEM_BINARY_0 @"/mkmon_A_192k.dsp"
00304 #define DSP_64K_SPLIT_MUSIC_SYSTEM_0 @"/mkmon_A_192k.lod"
00305 
00306 /* Number of words above which array-reads from the DSP use DMA */
00307 #define DSP_MIN_DMA_READ_SIZE 0
00308 
00309 /* Number of words above which array-writes to the DSP use DMA */
00310 #define DSP_MIN_DMA_WRITE_SIZE 128
00311 
00312 /* New mode for more efficient DMA transfers (cf. <nextdev/snd_msgs.h>) */
00313 #define DSP_MODE32_LEFT_JUSTIFIED 6
00314 
00315 #ifndef MAX
00316 #define  MAX(A,B)       ((A) > (B) ? (A) : (B))
00317 #endif
00318 
00319 #ifndef MIN
00320 #define  MIN(A,B)       ((A) < (B) ? (A) : (B))
00321 #endif
00322 
00323 #ifndef ABS
00324 #define  ABS(A)         ((A) < 0 ? (-(A)) : (A))
00325 #endif
00326 
00327 #define  DSP_MALLOC( VAR, TYPE, NUM ) ((VAR) = (TYPE *) malloc( (unsigned)(NUM)*sizeof(TYPE)))
00328 
00329 #define  DSP_REALLOC( VAR, TYPE, NUM )                          \
00330    ((VAR) = (TYPE *) realloc((char *)(VAR), (unsigned)(NUM)*sizeof(TYPE)))
00331 
00332 #define  DSP_FREE( PTR ) free( (char *) (PTR) );
00333 
00334 /* Exports from DSPGlobals.c */
00335 
00336 // this is declared above
00337 //MKDSP_API int DSPErrorNo;              /* Last DSP error */
00338 MKDSP_API int DSPDefaultTimeLimit;  /* Default is 1000 which is 1 second */
00339 MKDSP_API int DSPAPTimeLimit;    /* Default is 0 which means "forever" */
00340 // this is declared above with DSPTimeStamp. Which is right?
00341 // MKDSP_API DSPFix48 DSPMKTimeStamp0; /* Always {0,0} (tick-synchronizer) */
00342 
00343 #define DSP_ATOMIC 1
00344 #define DSP_NON_ATOMIC 0
00345 #define DSP_TIMEOUT_FOREVER 0
00346 #define _DSP_MSG_READER_TIMEOUT 30
00347 
00348 #endif /* DSP_H */
00349 
00350 #endif
00351