Difference between revisions of "Kernel Normal Form"

From Embedded Xinu
Jump to navigation Jump to search
Line 20: Line 20:
  
 
== Types ==
 
== Types ==
To avoid machine type size issues, the ''int'' and other #define types are deprecated.  Instead, all code should use: <code>long</code>, <code>ulong</code>, <code>short</code>, <code>ushort</code>, <code>char</code>, <code>uchar</code>.
+
To avoid machine type size issues, the <code>int</code> and other #define types are deprecated.  Instead, all code should use: <code>long</code>, <code>ulong</code>, <code>short</code>, <code>ushort</code>, <code>char</code>, <code>uchar</code>.
  
 
Pointers which are used soley as references to memory locations should be declared of type <code>void *</code>.
 
Pointers which are used soley as references to memory locations should be declared of type <code>void *</code>.

Revision as of 17:37, 2 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.

/* exampleFile.c - exampleFile, fileLoaded, getIO                 */
/* Copyright (C) 2007, Marquette University. All rights reserved. */

#include <file.h>

/* End of file marker */
#define FILE_END 0x255

/*
 * Processor Registers
 */
#define zero, $0  /* hardwired zero  */
#define at,   $1  /* assembler temp. */
#define s0,   $16 /* callee saved    */

extern unsigned long long getData(int *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(int fd, int timeout)
{
    int counter;             /* number of cycles we've been waiting   */
    unsigned long long 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(int 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
 */
unsigned long long getIO()
{
    unsigned long long data;

    data = getData(s0);

    return data;
}