Difference between revisions of "Kernel Normal Form"
Jump to navigation
Jump to search
(→Example: Updated example) |
|||
Line 35: | Line 35: | ||
It should be noted that since HTML does not allow the tab character, this example should reflect someone who has their tab stop set to 4 spaces. USE THE \t CHARACTER. | It should be noted that since HTML does not allow the tab character, this example should reflect someone who has their tab stop set to 4 spaces. USE THE \t CHARACTER. | ||
− | /* exampleFile.c | + | /** |
− | /* Copyright (C) 2007 | + | * @file exampleFile.c |
+ | * @provides exampleFile, fileLoaded, getIO | ||
+ | * While only an example this portion should contain general information | ||
+ | * about the importance of this file. | ||
+ | * | ||
+ | * $Id$ | ||
+ | */ | ||
+ | /* Embedded XINU, Copyright (C) 2007. All rights reserved. */ | ||
− | #include < | + | #include <kernel.h> |
+ | #include <proc.h> | ||
+ | #include <queue.h> | ||
/* End of file marker */ | /* End of file marker */ | ||
− | #define FILE_END 0x255 | + | #define FILE_END 0x255 /**< Magic number to designate end-of-file */ |
/* | /* | ||
* Processor Registers | * Processor Registers | ||
*/ | */ | ||
− | #define zero, $0 | + | #define zero, $0 /**< hardwired zero */ |
− | #define | + | #define a0, $4 /**< first passed argument */ |
− | #define s0, $16 /* callee saved | + | #define s0, $16 /**< callee saved */ |
− | extern | + | extern ulong getData(int *register); |
+ | extern ulong data_register; | ||
/** | /** | ||
* Load an example file into XINU. | * Load an example file into XINU. | ||
− | * @param fd descriptor of file to load | + | * @param fd descriptor of file to load |
* @param timeout how long to wait before error. | * @param timeout how long to wait before error. | ||
* @return OK for success, SYSERR for failure to load | * @return OK for success, SYSERR for failure to load | ||
*/ | */ | ||
− | + | devcall exampleFile(ushort fd, ulong timeout) | |
{ | { | ||
− | + | ulong counter; /* number of cycles we've been waiting */ | |
− | + | ulong data; /* 64-bits for storing a section of data */ | |
for ( counter = 0; counter < timeout; counter++ ) | for ( counter = 0; counter < timeout; counter++ ) | ||
Line 82: | Line 92: | ||
* @return OK if complete, SYSERR if incomplete | * @return OK if complete, SYSERR if incomplete | ||
*/ | */ | ||
− | + | local fileLoaded(ushort fd) | |
{ | { | ||
/* check for end of file marker */ | /* check for end of file marker */ | ||
Line 101: | Line 111: | ||
* @return data from register s0 | * @return data from register s0 | ||
*/ | */ | ||
− | + | ulong getIO() | |
{ | { | ||
− | + | switch (data_register) | |
− | + | { | |
− | + | case S0: case S1: | |
− | + | return getData(s0); | |
− | return | + | case A0: case A1: case A2: case A3: |
+ | return getData(a0); | ||
+ | default: | ||
+ | return SYSERR; | ||
+ | } | ||
} | } |
Revision as of 23:31, 18 July 2007
Tabs
- Use tabs to indent code.
- Use spaces to position comments that appear to the right of code.
Braces
- For functions and control blocks, the brace goes on its own line.
- Control blocks can be compressed to
Spaces
- Spaces around operators are recommended, around
=
are required. - The parenthesis following a keyword is always prefixed by a space.
- Following a function call or declaration, there is no space between the name and the opening parenthesis.
Function Naming
- Function names use Java style lowerCamelCase, following Java conventions on capitalization edge cases.
- Avoid unnecessary abbreviation in function names as reasonable.
Global Variables
- Global tables are named after the related structure with the suffix "tab", such as
devicetab
,processtab
, etc.
Types
To avoid machine type size issues, the int
and other #define types are deprecated. Instead, all code should use: long
, ulong
, short
, ushort
, char
, uchar
.
Pointers which are used soley as references to memory locations should be declared of type void *
.
Comments
- The top of any file should contain a comment with the file name and major public functions (as well as the copyright once it is determined)
- Functions should follow normal javadoc (doxygen in our case) style
- Comments should be follow /* ... */ style and vary depending on line size
Example
It should be noted that since HTML does not allow the tab character, this example should reflect someone who has their tab stop set to 4 spaces. USE THE \t CHARACTER.
/** * @file exampleFile.c * @provides exampleFile, fileLoaded, getIO * While only an example this portion should contain general information * about the importance of this file. * * $Id$ */ /* Embedded XINU, Copyright (C) 2007. All rights reserved. */ #include <kernel.h> #include <proc.h> #include <queue.h> /* End of file marker */ #define FILE_END 0x255 /**< Magic number to designate end-of-file */ /* * Processor Registers */ #define zero, $0 /**< hardwired zero */ #define a0, $4 /**< first passed argument */ #define s0, $16 /**< callee saved */ extern ulong getData(int *register); extern ulong data_register; /** * Load an example file into XINU. * @param fd descriptor of file to load * @param timeout how long to wait before error. * @return OK for success, SYSERR for failure to load */ devcall exampleFile(ushort fd, ulong timeout) { ulong counter; /* number of cycles we've been waiting */ ulong data; /* 64-bits for storing a section of data */ for ( counter = 0; counter < timeout; counter++ ) { data = getIO(); /* TODO: perform operation on data */ /* We have finished reading the file */ if ( loadXinu(fd) == OK ) { return OK; } } return SYSERR; } /** * Search for the end of the file * @param fd file descriptor to finish reading * @return OK if complete, SYSERR if incomplete */ local fileLoaded(ushort fd) { /* check for end of file marker */ if ( filetab[fd].buffer == FILE_END ) { /* other code if needed */ return OK; } else { /* other code if needed */ return SYSERR; } } /** * Read data from the s0 register through an assembly function. * @return data from register s0 */ ulong getIO() { switch (data_register) { case S0: case S1: return getData(s0); case A0: case A1: case A2: case A3: return getData(a0); default: return SYSERR; } }