Monday, April 28, 2008

Calculating remaining time


It might sound strange, but one feature I really like in an application is the ability to estimate remaining time. Wheter it is the time it will take to install an application, copy a file or perform another lengthy process.

The last time I came across such an application was while installing TrueCrypt. It has a fantastic timer which updates itself continously during the installation.

Here is a method I wrote for Cosmos for determining how much time remains for compiling the operating system. The method takes two arguments, one for how far we've come in the process, and one for the total length of the process.

The major strength of this method is that it continously recalculates how much time remains. So no more "1 second remaining" for minutes, or grossely missing the target by estimating years to copy a single file.

public static System.Diagnostics.Stopwatch xBuildTimer;
public TimeSpan CalculateRemainingTime(int completedCount, int totalCount)
{
    if (xBuildTimer == null)
    {
        xBuildTimer = new System.Diagnostics.Stopwatch();
        xBuildTimer.Start();
    }
 
    long percentComplete = (completedCount * 100 / totalCount);
    if (percentComplete == 0) //Avoid Divide by Zero
        percentComplete = 1;
 
    long remaining = (xBuildTimer.ElapsedMilliseconds / percentComplete) 
        * (100 - percentComplete);
 
    return new TimeSpan(remaining * 10000);
}

Saturday, April 5, 2008

The joy of Extension Methods in C# 3.0

I discovered a new addition to C# 3.0 called Method Extension while I was writing a convertor for Hex and Binary for Cosmos today. Method Extension is a technique for "spot-welding" methods on to existing classes or types.


    //Without C# 3.0 Method Extension
    if (IsOdd(7))
        ;
 
    //With C# 3.0 Method Extension
    if (7.IsOdd())
        ;


Here the I've added the IsOdd method onto an integer type. I think the lower example is a lot easier to read than the upper.

Adding an extended method is as simple as adding 'this' keyword on the first parameter on a static method inside a static class, like this:


public static class IntExtender
    {
        /// 
        /// Checks if number is odd or even.
        /// 
        public static bool IsOdd(this int n)
        {
            if ((n % 2) > 0)
                return true;
            else
                return false;
        }
    }


Method Extensions requires a reference to System.Data.Core assembly in your project.


The Intellisense looks like this:














By adding extensions in Cosmos we can now directly convert numbers to Hex and Binary like this:

foreach (byte b in bytes)
    {
        Console.Write(b.ToBinary());
        Console.Write(b.ToHex());
    }

Wednesday, March 26, 2008

Progress and whatnot

Those who really know me, i.e. my precious Julie, know that when I find something I find funny I do it. A lot.

So for the last few days I've been totally focused on Cosmos, and have made some significant changes dare I say. The network driver for RTL 8139 became stable enough to send proper packets the other day. Yey!

Today I added support for VMWare Server (since we only had VMWare Workstation from before). The VMW Server hasn't got an RTL network card, so for now we are only able to send networkpackets from Qemu and physical machines with RTL NIC's.

Oh, did I mention that the RTL can't receive packets yet? A mono-NIC if you will. Once I understand IRQ's in Cosmos (and we get that darn "Invalid Opcode" bug out of the way) we can hopefully get a fully functional network card.

It would be awesome if we could get two Cosmos machines talking to each other before Chad goes to Egypt to present Cosmos at Microsoft Egypt's EDC, Egyptian Developers Conference in April.

Saturday, March 22, 2008

Mention on the Cosmos blog


The Cosmos project has a blog - and darnit - I got my 15 minutes of fame.

For those of you who doesn't yet know what Cosmos is it's a framework for writing Operating Systems in C#. Within minutes you can have your own custom operating system up and running. The C# code you write is converted into IL by Visual Studio, and then converted to Assembly/Machine code by Cosmos.

Today Chad "Kudzu" Hower, a Microsoft MVP and one of the founders of the Cosmos project, implemented USB support. That means that you build directly from Visual Studio 2008 onto a USB memorystick, and then you can boot your Operating System directly from the memorystick.

Network support is still more unstable than a cow balancing on a 15 feet pole stacked on a football, but still...

Thursday, March 20, 2008

Network packet sent from Cosmos

I've been working on network support in Cosmos for some days now, and actually got a breakthrough this evening. A packet, although with invalid data, has been sent onto a virtual network!

Here is a screenshot of Wireshark (former Ethereal) which sniffs a packet from a virtual TAP -WIN32 networkcard connected together with Cosmos' RTL8139C+ NIC.


The next step is to get correct data to be sent, and also to receive packets.

Friday, March 14, 2008

Cosmos - an interesting place to be



Like before I've discovered new and interesting things through the blog of Scott Hanselman. This time I've stumbled over Cosmos - an operating system written in C#.

I've been fascinated with creating OS's for several years. Back in 2004, I think it was, I wrote a simple OS in plain C. It didn't really do much except boot and detect some hardware.

Last summer I dabbled a little with the ReactOS operating system, and made some small sourcecode contributions there. That was C/C++, and unfortunately those are languages I'm not fluent in, so the development just progressed to slowly.

Now though I've found Cosmos which lets me use my favorite language, C#. Yey!

My goal is to create Network support in Cosmos. I've been working on a driver for the RTL8139 chipset for a few days now. Since I have a background in electronics I really love working in the sweet spot just between software and hardware. Working out bits and bytes, and addressranges and memoryareas. Bitfiddling and bitwise mathematics. Good fun!

Saturday, January 12, 2008

New R/C helicopters



I've been fighting the urge of buying a radiocontrolled helicopter for a month or two, and last week I had to succumb. I picked up a small Superfly 2.0 chopper at a local store, and have been playing with it a lot since then.

Today my boss and I went to a local hobby-store in Drammen, and ended up leaving with a Twister Bell 47 chopper each! The Bell is about 3 times as big as the Superfly, and 6 times as expensive.

The Superfly is extremely simple to fly, and can take quite a beating. It's been sent head-first into walls and furniter time and time again, but minimal damage. I even (crash)landed into a 500W lamp. Two channel controllersticks - dead easy.

I've been hovering the Bell around the livingroom this evening, and this helicopter is in another league. There are way more axis to control, and it takes some getting used to. I've had two very minor crashes, and both have resulted in broken rotorblades. Four channel controllersticks - hard, but fun!