ST-061793-09-R1 Sega Enterprises, Ltd. . THE C LANGUAGE CODING STANDARD PRELIMINARY DRAFT REV. 0.11 | JUNE 1 1993 HARDWARE R & D GROUP SYSTEM SOFTWARE DIVISION —1— Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. xxx.h header file The header file corresponds to xxx.c code file and defines public routine prototype within xxx.c or data type. The use of space * Do not insert space around binary operators such as "апа “-> abc.xyz _abcPtr->xyz • Insert one space after the reserved words. for (;;) switch (iSw) * Do not insert space between () after the function call. subFunction(); “wn u. » Insert one space before the “,” ог"; function(para1, рага2, рага3); for (n = 1; n « SIZE; n++) * Insert one space before and after assignmeni operators and comparison operators. а=р+с; м “_ ES • Insert no space between unary operators “++”, , and variables. ++; js * Insert no space before “)]” and after “({” brackets. xla + c] = 3 + (y + 2); Precedence and order of evaluation Add () consciously when the precedence in the expression is not clear. Using sizeof operator Set the size of the struct, union, and array with the sizeof operator. Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. (8) Control character When using control characters in storing, etc., do not input the code directly. Use V for tab and 133 for escape. (9) Number of statements per line Each line includes only one statement. Change to a new line for the next statement, such as an if statement—and a null statement if (a == b) а - С; for (n -0; array[n] !- 0; n++) (10) Do not evaluate numeric operations as logical operations if (a + b) if (1а + b) Incorrect if (a+ b Il 0) if (a+ b == 0) Correct (11) Tab size Each tab size consists of 8 spaces. (12) Control sentence format Ranking by {} starts from the upper right and ends at the lower left. 4 spaces are set as 1 indent unit. ' e if statement f (conditional statement) (conditional statement) { if (conditional statement 1) statement; statement 1; -statement 1; ) eise í } else if (conditional statement 2){ statement; statement 2; statement 2; ) }else { statement 3; } * for statement for (statement 1; statement 2; statement 3) ( statement; ) --4-- Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. * while statement while (conditional statement) ( statement; ) * do statement do | statement; ) while (conditional statement); * switch statement switch (statement) { | switch (statement) { саѕе 0: /* comment */ саѕе 0: /* comment "/ statement 1; statement 1; break; break; default: /* comment "/ default /* comment "/ statement 2; statement 2; break; break; e | | (13) Соттепі To exchange the source code internationally, comments are inserted in English. In principle, when using Japanese, EUC code is used with UNIX, and Shift-JIS is used with MS-DOS. However, this may change depending on the environment. | * Block comment /* к Comment align /* column position and */ column position. * Statement comment Statement / Comment */ RE Translated by J.T. /August 11, 1993 o Р ST-061793-09-R1 Sega Enterprises, Ltd. * Comment to ignore part of the code #if O Code set #endif (14) Identifier « Specify identifiers in 32 characters or less and describe them fully or near- fully spelled. For functions, use names in the form of verb+name which | invoke the action. Markoff and punctuation using upper case letters and “-" should be consistent within the same project. * Local variable name Upper case letters are used in variable names to separate single words. localVariableName local. variable name local. Variable Name * Global variable name or data type name Upper case letters are used in variable names to separate single \ words and as leading characters. Sint16 . GlobalVariableName Global. variable name Global. Variable Name * Macro name Macro names аге all in upper case letters and separated by "-". MY MACRO, NAME (15) Restrictions * Direct specification with binary operators such as (".", “,” ,and “->”) shall not exceed 4 steps. vali Ptr->val2Ptr->val3Ptr->valName is good! | . . --6-- Translated by J.T. /August 11, 1993 3.2. 2) ST-061793-09-R1 Sega Enterprises, Ltd. . ро пої write to constant data area. str1Ptr = "abcdefg" ; strcpy(str1Ptr, str2Ptr); » When dividing a signed variable by a power of 2, the operation is not carried through the right shift. The MSB state of a signed variable during the right shift is not defined by ANSI-C. ‚ * Do not nest the header file (include header within header). Declaration: Declaration of data type * The leading character of the struct name shall be in the upper case, and when declaring the struct as typedef, the struct name shall be identical to the typedef name. typedef struct StructName ( біпії6 маг1; / comment */ Sint8 var2; /" comment "У ) StructName; * Do not use the basic data type (char, int, short, etc.) prepared by the C language; use virtual basic data type (such as PRISES Float з declared by xport.h. š Constant data * Main constant data is declared by define statement. * Constant data to s secure variable area and parameters for referencing only, are const declared. const Sinti6 iConstData = 10; char *strcpy(char *, const, char *); Restrictions * Do not use register variables. • Do not use bit field definition. --7-- Translated by J.T. /August 11, 1993 Ue ST-061793-09-R1 Sega Enterprises, Ltd. 4. Coding Example The following keywords embedded within the source are to be used with the UNIX text revision control tools (Revision Control. System or RCS) and data corresponding to each keyword within the text will be automatically embedded when checked out from the RCS. $Author$ = Login name of the user who stored revision $Date$ - Date when revision was stored $Header$ = RCS file name, revision number, date, author, standard header which includes the status $Locker$ = Login name of the user who locked the revision $Log$ = This is a log message during storage and is inserted in sequence when updating revision $Revision$ = Revision number assigned to revision $Source$ = Full bus name for RCS file $State$ = Status assigned to revision ST-061793-09-R1 Sega Enterprises, Ltd. 4.1 template.c code file /я--“--<---------------------------------------------------------------------- * FILE: template.h ” $author$ * $Date$ ы $Lockor$ ы $Revision$ ` $Source$ * $State$ ж А, 2 * Copyright (c) by SEGA Enterprises, Ltd. 1993. All right reserved. Y + PURPOSE: ` General description of program file * DESCRIPTION: + How to use the module and detailed description of the data format * INTERFACE: The list and description of global variables and global functions defined in this file as an external interface . * < GLOBAL VARIABLES LIST > š PublicDatal - description of PublicbDatal Translated by J.T. /August 11, 1993 ` Sega ST-061793-09-R1 Enterprises, Ltd. . < FUNCTICNS LIST > * main - rain reutine з CAVEATS: * ‘ For the description of remarks * AUTHOR(S) : * ° List of author(s). modifier(s). etc. * » HOD HISTORY: * $1055 * of Й з . C STANDARD LIBRARY FLNCTICNS/MACROS DEFINES “/ шдеГіпе XPRIIITF — Definitions of standard library usage routine names sinclude "xstdlib.h" - -- Definitions of standard library fe ‚` С VIRTUAL ТІРЕ5 DEFINITIONS ./ Шіпсішіе “xport.h" — Definitions of virtual basic data types з /* ` є USER SUPELIED INCLUDE FILES | «/ Sinclude "tenplate.h" o ТроЧайпиют о user inciude tiles /* > GUCBAL DECLARATIONS of “inis PublicRatatl (51; /* + t2ZAL ГЕҒІПТА/МАСАЛ5 M ?define ПМАХ-ПО 10 /* Carmrmam */ ' —30— Transtaled by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. /* е STATIC DECLARATIONS “/ static Uint8 *resid = "SHoader: /home/ssdg/nasat/psat/doc/RCS/cstand.tex,v 1.1 1993/05/28 Program header data from the Control System Uinti6 StaticData; /* Comment */ /* a STATIC FUNCTION PROTOTYPE DECLARATIONS ` I ./ static PROTOTYPE(Sinti6, plus, (Sinti6, Sint16)); static FRÜTOTYPE(Sinti6, minus, (Sint16, Sint16)); -L f ІГечетучосовьновьвововеефавоввєєсввнтововнонановватосевовюаньовваваннявчавнвови є ә NAME: main() - main routine — The routine names and overview * PARAMETERS : = The description ої parameters . (1) Sint16 argc - number of input command's argument. . (2) Char ^ **argv - argument string array. * DESCRIPTION: The description of routine functions » PRECONDITIONS: . The description of any preconditions such as changes in the intpul parameters. . global variables, etc.. before the execution * POSTCONDITIONS: s ‚© » ` The description of any changes in the return values. output parameters. global ` variables. etc.-after the execution -~ 22 . « CAVEATS: ` ` : The description oi remarks "«назовавнначовотичнввнннозловевевавовочвазозчововкочовувочовкувавввнко воно» “/ Sínt16 | zain(Sirt16 argc, Char seargv) — Local vanables сетот Sinti6 i; Sint16 j; oat __ Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. бір+іб k; /* COMMENT */ /** BEGIN X4OOOROOOOODOOOODOOOODEOOODOOOOOOOOOOR о кажа printf("\nstart template\n") ; /* * process 1 +/ for (i = 0, j = MAX_NO; i < MAX_NO; i++, j--) { if ((i % 3) == 0) { x= plus(i, 3): printf("i(Zd) т j(Ad) = ўа\п", 1, j, к); ) else if (Gi 7 3) == D (1 ) k = ninus(i, j); ргіпет("1034) - JOD = Тала", i, j, XX: ) eise 5 k = minus(j, i); printf('"j(4d) - 104) = d\n", j; i, k); /* * process 2 «/ | í > O; j = MAX_NO; s while (i < MAX_NO) 4 switch (1323) €. uo | case 0: /* COMMENT*/ k = plus(i, DE э IRAM printf("iCid) + jOa) = ча", 1, j. 30: break; | case 1: /* COMMENT */ k = ninus(i, j); printf("i(%d) - 1008) = Мала", i, j, X); break; I і default: /* COMMENT «/ k з minus(j, i); printf("jC4d) - iha) = d\n", j, i, к); " break; | \ { й —12— Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. } printf("end template\n") ; return ОК; "L [42984409 458 EERESEXTERRKREESREEXERETEEEEERER ERR REA RARE EE а L * NAHE: plus() - plus prosess « PARAMETERS : * (1) SintiG а - «і» parameter 1. я (2) Sint16 b - «i» parameter 2. * DESCRIPTION: * compute a * b and return result value. » POSTCONDITIONS: * (1) Sinti6 result ~ return value. ж Qutawasi... GREE SSR EEE ODES SEEEOEEOHETESESEERSRERERSSSSS SEESH NERS SSS -/ static Sint16 IRE plus(Sinti6 а, біпсі b) Баха C . < /аж ВЕСІМ LARESETXTEKEOARAUWSSANKIRYNETAVEERESERESSESSEREFEAXSRSE SECOS eaa AR / і return a * b; ^L [95358953095 E CVV e жа кожоке жек» бэк жебе» жеке rmn в + NAME: шіпизО ~ minus prosess * PARAHETERS * (1) SintiG а - parameter 1. * (2) Sinti6 b - parameter 2. ж ж DESCRIPTION: М compute а - b and return result value. eL Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. « POSTCONDITIONS: * (1) Sinti16 result - return value. - ————————— OPI ./ static Sinti6 minus(Sinti6 a, Sinti6 b) A { /** BEGIN ————————————————— МЫ return a -= bj. —144- Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. o 4.2 template.h Include flle |вя«-------------------------------------------------------- ------------------ * FILE: template.h ` $Author$ * $Date$ . $Locker$ * $Revision$ * $Source$ * $State$ * * Copyright (c) by SEGA Enterprises, Ltd. 1993. All right reserved. E . | : * PURPOSE: General description of “include file". + DESCRIPTION: How to use "Include Ше“ and description of data format o • AUTHOR(S): . List of authors, modifiers, etc. * HOD HISTORY: . $Log$ #ifndef ТЕМРІАТЕ Н &define TEMPLATE_H - Setting Include flags /* « GLOBAL DEFINES/MACROS DEFINES Setting a flag for the included file * / $define TEMPLATE.DEF 1 /* а STRUCTURE DECLARATIONS */ o struct NeuStructi { /* comment */ — b — Translated by J.T. /August 11. 1993 ST-061793-09-R1 Sega Enterprises, Ltd. Sinti6 vari; — /* comment =/ Sinti6 уаг2; /* comment »/ y; struct NewStruct2 ( /* comment */ struct NewStructi *nevStructi; /* comment*/ Sinti6 vari; /* comment#/ 518016 var2; 2 /* comment «/ /* * TYPEDEFS “/ typedef struct NewStruct2 NewStruct2; /* « EXTERNAL VARIABLE DECLARATIONS e " ж EXTERNAL FUNCTION PROTOTYPE DECLARATIONS ./ М ` - extern PROTOTYPE(Sinti6, main, (Sinti6, Char**)); fendif /* ifndef TEMPLATE.H »/ \ —16— Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. 4.3 xstdlib.h standard library Include Пе /ж-“"“-““---------------------------------------------------------------------- * FILE: xstdlig.h . * $Author$ e з $Date$ . $Locker$ * $Revision$ * $Source$ * $5tate$ a * Copyright (c) by SEGA Enterprises, Ltd. 1993. All right reserved. * * PURPOSE: в ы standard liblary header files for different С environment. а: DESCRIPTION: © «Usage» %4еТіпе ХРАЇХТЕ <-- use function name із "printf". .. 7 idefine XSTRCPY ч i " cee . ` finclude "xstdlib.h" * a * AUTHOR(S): i * e * H.Endo Hardware RED Dept. System Software Section ü = HOD HISTORY: * $Log$ fifdef XPRINTF tdefine L.STDIO.H fendif “ifdef XSTRCPY Sdefine L STRING H #ondif #ifdef L.STDIO.H ees 72 Translated by J.T. /August 11, 1993 ST-061793-09-R1 Sega Enterprises, Ltd. #ifndef ХЗТРІОН #define XSTDIO_H #include fendif #ondif ifdef L.STRING.M : #ifndef XSTRING.H define XSTRING_H Sínclude . Translated by J.T. /August 11, 1993 ((Fixed32)((x) * 65536.0)) ((Fixed32)((x) * 65536)) ((Sint32)((x) / 65536)) ((Float32)((1ong)(x) / 65536.0)) (((а) / 256) ж ((ь) / 256)) (Ca / C(b) / 256)) * 256) (((х) < 0)? -(х) : (х)) (C(x) < (y)) 7 (у): (x)) (C(x) < (у)) 7 (х) : (2) —21—